In simple words, try/catch block within a try block is known as nested try/catch block. Syntax of the nested try/catch block is given below:
Read Also: finally block in java
Syntax of nested try/catch block
... // Main try block try { statement1; statement2; // Inner try block try { statement1; statement2; } catch(Exception ex) { //code } } catch(Exception ex) { //code } ...
Why do we need nested try/catch block?
Sometimes a situation may arise where a few statements of a block may cause one exception and the remaining statements may cause another exception as shown below in the example.Example of Nested try/catch block
public class NestedTryCatchExampleOne { public static void main(String args[]) { // Main try block try { System.out.println("Inside main try block"); // Inner try-catch block1 try { System.out.println("Dividing"); int num = 1989/0; } catch(ArithmeticException ex) { System.out.println(ex); } // Inner try-catch block2 try { int[] arr = new int[6]; arr[7] = 8; } catch(ArrayIndexOutOfBoundsException ex) { System.out.println(ex); } System.out.println("remaining statements inside main try block"); } catch(Exception ex) { System.out.println(ex); } System.out.println("remaining code, normal flow"); } }
Output:
Inside main try block
Dividing
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 6
remaining statements inside main try block
remaining code, normal flow
Explanation
In the above example, you can see that there are two inner try/catch blocks inside the main try block's body, marked as try-catch block1 and try-catch block2.Block1: In block1, we have divided the integer by zero. It produces ArithmeticException. Since the catch of block1 is handling the ArithmeticException, we get the output java.lang.ArithmeticException: / by zero.
Block2: In block2, we are accessing the 8th element of the array which is of size 6. Hence, it produces ArrayIndexOutOfBoundsException. Since the catch of block2 is handling the ArrayIndexOutOfBoundsException, we get the output java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 6.
Points to Remember
1. try/catch block can be nested insidea. try block
b. catch block
c. finally block
2. Each time an inner try block does not have a catch/finally handler for a particular exception, then the catch blocks of the parent try block are checked for that exception, if a match is found then that catch block statements execute. If the match is not found even in the parent catch block then the program will terminate abruptly showing system generated message. Let's understand this with the help of an example:
public class NestedTryCatchExampleTwo { public static void main(String args[]) { // Main try block try { System.out.println("Inside main try block"); // Inner try-catch block1 try { System.out.println("Dividing"); int num = 1989/0; } catch(ArithmeticException ex) { System.out.println(ex); } // Inner try-catch block2 try { int num2 = 100/0; } catch(ArrayIndexOutOfBoundsException ex) { System.out.println(ex); } System.out.println("remaining statements inside main try block"); } catch(Exception ex) { System.out.println(ex); } System.out.println("remaining code, normal flow"); } }
Output:
Inside main try block
Dividing
java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
remaining code, normal flow
Explanation
You can see that the ArithmeticException occurred in the inner try/catch block2. Since try-catch block2 is not handling this exception, so, control then gets transferred to the main try-block where it found the appropriate catch block for an exception. If the main try/catch block fails to handle the exception then the program will terminate abruptly, that is not the case here. Hence, the above example prints remaining code, normal flow.That's all for today, please mention in the comments in case you have any questions related to nested try-catch block in Java.