As the name suggests, a try block within a try block is called nested try block in Java.
Why to use nested try block
This is needed when different blocks like outer and inner blocks may cause different errors. To handle them we need nested try blocks.
How exception handling works in nested try block
When nested try blocks are used, the inner try block is executed first. Any exception thrown in the inner try block is caught in the corresponding catch block. If a matching catch block is not found, then catch block of the outer try block are inspected. This process continues until all nested try statements are exhausted. If no matching catch block found, program terminates abnormally.
Syntax of nested try block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
try //Outer try block { //Some Statements try //Inner try block { //Some Statements } catch (Exception ex) //Inner catch block { } } catch(Exception ex) //Outer catch block { } |
Example1 – nested try block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.vidvaan.corejava.exception10.nestedTry; // nested try catch block class Example01NestedTry { // main method public static void main(String args[]) { // Main try block try { // initializing array int a[] = { 1, 2, 3, 4, 5 }; // trying to print element at index 5 System.out.println(a[5]); // try-block2 inside another try block try { // performing division by zero int x = a[2] / 0; } catch (ArithmeticException e2) { System.out.println("division by zero is not possible"); } } catch (ArrayIndexOutOfBoundsException e1) { System.out.println("ArrayIndexOutOfBoundsException - Element at such index does not exists"); } } // end of main method } |
Output :
ArrayIndexOutOfBoundsException – Element at such index does not exists
Example2 – nested try block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.vidvaan.corejava.exception10.nestedTry; // nested try catch block class Example02NestedTry { // main method public static void main(String args[]) { // main try-block try { // try-block2 try { // try-block3 try { int arr[] = { 1, 2, 3, 4 }; System.out.println(arr[10]); } // handles ArithmeticException if any catch (ArithmeticException e1) { System.out.println("Arithmetic exception - try-block1"); } } // handles IllegalArgumentException if any catch (IllegalArgumentException e2) { System.out.println("IllegalArgumentException - try-block2"); System.out.println(""); } } // handles ArrayIndexOutOfBoundsException if any catch (ArrayIndexOutOfBoundsException e3) { System.out.print("ArrayIndexOutOfBoundsException - main try-block"); } catch (Exception e4) { System.out.print("Exception - handled in main try-block"); } } } |
Output :
ArrayIndexOutOfBoundsException – main try-block
Is the following nested try-catch block is an anti-pattern?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
try { //do something } catch (Exception e) { try { //do something } catch (Exception ex) { try { //do something } catch (Exception e1) { //More try catches? } } } |
This is sometimes unavoidable, especially if your recovery code might throw an exception. Not pretty, but sometimes there are no alternatives.