Exception: Any Code that disturb the normal flow of the program is called Exception.
Exception Handling:
It is a mechanism to handle compile and run time Exceptions. try catch finally throw and throws are the keywords are used to handle the Exceptions in your source code
Exception Hierarchy:
Advantage of Exception Handling:
It will not terminate in the middle or crash your application. It will run your application smoothly.
Exception Classes are categorise mainly into two parts:
e.g.
throws:
It is used to declare the Exception after method signature.
e.g.
What is difference between throw and throws?
Custom Exception:
You can create your own exception class by extending Exception or its subclasses.
e.g.
Output:
Loan of 200000.0 is sanction
class com.java.exception.BankLoanException Loan of 90000.0 is not sanction
Exception Handling:
It is a mechanism to handle compile and run time Exceptions. try catch finally throw and throws are the keywords are used to handle the Exceptions in your source code
Exception Hierarchy:
Advantage of Exception Handling:
It will not terminate in the middle or crash your application. It will run your application smoothly.
Exception Classes are categorise mainly into two parts:
- Checked Exception: It is also called compile time Exception. Except Error and RuntimeException all other Exception classes are Checked Exception
- Unchecked Exception: It is also called run time Exception. RuntimeException and its subclasses are Unchecked Exception.
Various ways to use try catch and finally blocks:
It is used to explicitly throw an Exception when some condition is true.
- try-catch blocktry {
//Code where there is chance to get any Exception
} catch(ExceptionClassName e){
//Handle the ExceptionClassName Code
}
e.g.
package com.java.exception;
public class ExceptionHandling {
public static void main(String[] args) {
try {
System.out.println("main Method is started");
System.out.println(5/0);
} catch (ArithmeticException e) {
System.out.println(e.getClass());
System.out.println("Detail About Exception is: " + e.getMessage());
}
System.out.println("main method is completed");
}
}
Output:
main Method is started
class java.lang.ArithmeticException
Detail About Exception is: / by zero
main method is completed
- try-finally blocktry {
//Code where there is chance to get any Exception
} finally {
//Write the closing database and IO file stream class Code
}
e.g.
package com.java.exception;
public class TryFinally {
public static void main(String[] args) {
System.out.println("Main method stated");
try {
TryFinally obj = null;
System.out.println(obj.hashCode());
} finally {
System.out.println("finally block will execute every time if exception occurs or not");
}
System.out.println("Main method terminated");
}
}
Output:
Main method stated
finally block will execute every time if exception occurs or not
Exception in thread "main" java.lang.NullPointerException
at com.java.exception.TryFinally.main(TryFinally.java:8) - try with multiple catch blocktry {
//Code where there is chance to get any Exception
} catch(MostSpecificException1 e){
//Handle the MostSpecificException1 Code
} catch(MostSpecificException2 e){
//Handle the MostSpecificException2 Code} catch(MostSpecificException3 e){//Handle the MostSpecificException3 Code} catch(Exception e){//Handle the Exception Code}
e.g.
package com.java.exception;
public class TryMultipleCatch {
public static void main(String[] args) {
System.out.println("Main method stated");
try {
TryMultipleCatch obj = null; //It will throw NullPointerException
obj = new TryMultipleCatch();
System.out.println("Hash Code value for Object of class TryMultipleCatch 'obj' is " + obj.hashCode());
int num = Integer.parseInt("abc", 16);
//num = Integer.parseInt("abc"); //It will throw NumberFormatException
System.out.println("Value in decimal format of \"abc\" is " + num);
System.out.println(10 / 0);
Thread.sleep(5000); //if any thread has interrupted the current thread
} catch (NullPointerException e) {
System.out.println(e.getLocalizedMessage());
} catch (NumberFormatException e) {
System.out.println("Enter string value in correct format");
} catch (ArithmeticException e) {
System.out.println(e.getClass() + " has occured");
} catch (Exception e) {
System.out.println("Some other Exception has occured" + e.getClass());
}
System.out.println("Main method terminated");
}
}
Output:
Main method stated
Hash Code value for Object of class TryMultipleCatch 'obj' is 366712642
Value in decimal format of "abc" is 2748
class java.lang.ArithmeticException has occured
Main method terminated - try catch and finally block
try {
//Code where there is chance to get any Exception
} catch(Exception e){
//Handle the Exception Code
} finally {
//Write the closing database and IO file stream class Code}
e.g.
package com.java.exception;
public class TryCatchFinally {
public static void main(String[] args) {
System.out.println("main Method is started");
try {
System.out.println(5/0);
} catch (Exception e) {
System.out.println(e.getClass() + " has occured");
} finally {
System.out.println("finally block will execute every time if exception occurs or not");
}
System.out.println("main method is completed");
}
}
Output:
main Method is started
class java.lang.ArithmeticException has occured
finally block will execute every time if exception occurs or not
main method is completed
- Nested try-catch block
try {
//Code where there is chance to get any Exception
try {
//Code where there is chance to get any Exception
} catch(ExceptionClass e) {
//Handle the ExceptionClass Code
}
} catch(ExceptionClassName e){
//Handle the ExceptionClassName Code
}
e.g.
package com.java.exception;
public class NestedTryCatch {
public static void main(String[] args) {
System.out.println("main Method is started");
try {
try {
System.out.println(5/0);
} catch (Exception e) {
System.out.println("Nested try catch");
throw e;
}
} catch (ArithmeticException e) {
System.out.println(e.getClass());
System.out.println("Detail About Exception is: " + e.getMessage());
}
System.out.println("main method is completed");
}
}
Output:
main Method is started
Nested try catch
class java.lang.ArithmeticException
Detail About Exception is: / by zero
main method is completed
throw:
e.g.
throw new ArithmeticException();
throws:
It is used to declare the Exception after method signature.
e.g.
void method() throws ArithmeticException, NullPointerException, IOException {
//Code inside method that might throw above declared Exceptions
}
//Code inside method that might throw above declared Exceptions
}
What is difference between throw and throws?
throw | throws |
---|---|
It is used to explicitly throw an Exception when some condition is true. | It is used to declare the Exception after method signature. |
It is followed by object | It is followed by Exception class or classes |
It is used inside methods | It is used after method signature |
You cannot throw multiple exception object | You can declare multiple exception classes |
Custom Exception:
You can create your own exception class by extending Exception or its subclasses.
e.g.
package com.java.exception;
class BankLoanException extends ArithmeticException {
private static final long serialVersionUID = 1L;
public BankLoanException(String text){
super(text);
}
}
public class BankLoan {
double amount;
public BankLoan(double amount) {
this.amount = amount;
}
public void isSanction(double amount){
if(amount <= 100000){
throw new BankLoanException(" Loan of " + amount + " is not sanction");
} else {
System.out.println("Loan of " + amount + " is sanction");
}
}
public static void main(String[] args) {
BankLoan loan = null;
try{
loan = new BankLoan(200000);
loan.isSanction(loan.amount);
loan = new BankLoan(90000);
loan.isSanction(loan.amount);
} catch (BankLoanException e) {
System.out.println(e.getClass() + e.getMessage());
}
}
}
class BankLoanException extends ArithmeticException {
private static final long serialVersionUID = 1L;
public BankLoanException(String text){
super(text);
}
}
public class BankLoan {
double amount;
public BankLoan(double amount) {
this.amount = amount;
}
public void isSanction(double amount){
if(amount <= 100000){
throw new BankLoanException(" Loan of " + amount + " is not sanction");
} else {
System.out.println("Loan of " + amount + " is sanction");
}
}
public static void main(String[] args) {
BankLoan loan = null;
try{
loan = new BankLoan(200000);
loan.isSanction(loan.amount);
loan = new BankLoan(90000);
loan.isSanction(loan.amount);
} catch (BankLoanException e) {
System.out.println(e.getClass() + e.getMessage());
}
}
}
Output:
Loan of 200000.0 is sanction
class com.java.exception.BankLoanException Loan of 90000.0 is not sanction
No comments:
Post a Comment