Exception Handling in Java
Exception Handling in Java
1. Exception Handling
2. Advantage of Exception Handling
3. Hierarchy of Exception classes
4. Types of Exception
5. Exception Example
6. Scenarios where an exception may occur
In this tutorial, we will learn about Java exceptions, it's types, and the
difference between checked and unchecked exceptions.
1. statement 1;
2. statement 2;
3. statement 3;
4. statement 4;
5. statement 5;//exception occurs
6. statement 6;
7. statement 7;
8. statement 8;
9. statement 9;
10. statement 10;
Do You Know?
o What is the difference between checked and unchecked exceptions?
o What happens behind the code int data=50/0;?
o Why use multiple catch block?
o Is there any possibility when the finally block is not executed?
o What is exception propagation?
o What is the difference between the throw and throws keyword?
o What are the 4 rules for using exception handling with method overriding?
1. Checked Exception
2. Unchecked Exception
3. Error
Difference between Checked and Unchecked
Exceptions
1) Checked Exception
2) Unchecked Exception
3) Error
Keyword Description
finally The "finally" block is used to execute the necessary code of the
program. It is executed whether an exception is handled or not.
JavaExceptionExample.java
Output:
1. int a=50/0;//ArithmeticException
1. String s=null;
2. System.out.println(s.length());//NullPointerException
1. String s="abc";
2. int i=Integer.parseInt(s);//NumberFormatException
If an exception occurs at the particular statement in the try block, the rest of
the block code will not execute. So, it is recommended not to keep the code
in try block that will not throw an exception.
1. try{
2. //code that may throw an exception
3. }catch(Exception_class_Name ref){}
1. try{
2. //code that may throw an exception
3. }finally{}
The catch block must be used after the try block only. You can use multiple
catch block with a single try block.
ADVERTISEMENT
ADVERTISEMENT
But if the application programmer handles the exception, the normal flow of
the application is maintained, i.e., rest of the code is executed.
ADVERTISEMENT
Example 1
TryCatchExample1.java
Output:
ADVERTISEMENT
Exception in thread "main" java.lang.ArithmeticException: / by zero
As displayed in the above example, the rest of the code is not executed (in
such case, the rest of the code statement is not printed).
There might be 100 lines of code after the exception. If the exception is not
handled, all the code below the exception won't be executed.
Example 2
TryCatchExample2.java
Output:
java.lang.ArithmeticException: / by zero
rest of the code
As displayed in the above example, the rest of the code is executed, i.e.,
the rest of the code statement is printed.
Example 3
In this example, we also kept the code in a try block that will not throw an
exception.
TryCatchExample3.java
Output:
java.lang.ArithmeticException: / by zero
Here, we can see that if an exception occurs in the try block, the rest of the
block code will not execute.
Example 4
Here, we handle the exception using the parent class exception.
TryCatchExample4.java
Output:
java.lang.ArithmeticException: / by zero
rest of the code
Example 5
Let's see an example to print a custom message on exception.
TryCatchExample5.java
Output:
Example 6
Let's see an example to resolve the exception in a catch block.
TryCatchExample6.java
Output:
25
Example 7
In this example, along with try block, we also enclose exception code in a
catch block.
TryCatchExample7.java
Output:
Here, we can see that the catch block didn't contain the exception code. So,
enclose exception code within a try block and use catch block only to handle
the exceptions.
Example 8
In this example, we handle the generated exception (Arithmetic Exception)
with a different type of exception class (ArrayIndexOutOfBoundsException).
TryCatchExample8.java
Output:
TryCatchExample9.java
Output:
java.lang.ArrayIndexOutOfBoundsException: 10
rest of the code
Example 10
Let's see an example to handle checked exception.
TryCatchExample10.java
ADVERTISEMENT
1. import java.io.FileNotFoundException;
2. import java.io.PrintWriter;
3.
4. public class TryCatchExample10 {
5.
6. public static void main(String[] args) {
7.
8.
9. PrintWriter pw;
10. try {
11. pw = new PrintWriter("jtp.txt"); //may throw exception
12. pw.println("saved");
13. }
14. // providing the checked exception handler
15. catch (FileNotFoundException e) {
16.
17. System.out.println(e);
18. }
19. System.out.println("File saved successfully");
20. }
21. }
Test it Now
Output:
Points to remember
ADVERTISEMENT
ADVERTISEMENT
o At a time only one exception occurs and at a time only one catch block
is executed.
o All catch blocks must be ordered from most specific to most general,
i.e. catch for ArithmeticException must come before catch for
Exception.
Example 1
Let's see a simple example of java multi-catch block.
MultipleCatchBlock1.java
Output:
Example 2
MultipleCatchBlock2.java
Output:
In this example, try block contains two exceptions. But at a time only one
exception occurs and its corresponding catch block is executed.
MultipleCatchBlock3.java
Output:
Example 4
In this example, we generate NullPointerException, but didn't provide the
corresponding exception type. In such case, the catch block containing the
parent exception class Exception will invoked.
MultipleCatchBlock4.java
Output:
Example 5
Let's see an example, to handle the exception without maintaining the order
of exceptions (i.e. from most specific to most general).
MultipleCatchBlock5.java
1. class MultipleCatchBlock5{
2. public static void main(String args[]){
3. try{
4. int a[]=new int[5];
5. a[5]=30/0;
6. }
7. catch(Exception e){System.out.println("common task completed");}
8. catch(ArithmeticException e){System.out.println("task1 is completed");}
9. catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 co
mpleted");}
10. System.out.println("rest of the code...");
11. }
12. }
Test it Now
Output:
Compile-time error