Nested try blocks in Exception Handling in Java
In Java, we can use a try block within a try block. Each time a try statement is entered, the context of that exception is pushed on to a stack. Given below is an example of a nested try.
In this example, inner try block (or try-block2) is used to handle ArithmeticException, i.e., division by zero. After that, the outer try block (or try-block) handles the ArrayIndexOutOfBoundsException.
Example 1:
class NestedTry { // 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"); System.out.println("Element at such index does not exists"); } } // end of main method } |
ArrayIndexOutOfBoundsException Element at such index does not exists
Whenever a try block does not have a catch block for a particular exception, then the catch blocks of parent try block are inspected for that exception, and if a match is found then that catch block is executed.
If none of the catch blocks handles the exception then the Java run-time system will handle the exception and a system generated message would be shown for the exception.
Example 2:
class Nesting { // 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 e) { System.out.println("Arithmetic exception"); System.out.println(" try-block1"); } } // handles ArithmeticException if any catch (ArithmeticException e) { System.out.println("Arithmetic exception"); System.out.println(" try-block2"); } } // handles ArrayIndexOutOfBoundsException if any catch (ArrayIndexOutOfBoundsException e4) { System.out.print("ArrayIndexOutOfBoundsException"); System.out.println(" main try-block"); } catch (Exception e5) { System.out.print("Exception"); System.out.println(" handled in main try-block"); } } } |
ArrayIndexOutOfBoundsException main try-block
Recommended Posts:
- Java | Exception Handling | Question 1
- Java | Exception Handling | Question 3
- Java | Exception Handling | Question 4
- Java | Exception Handling | Question 6
- Java | Exception Handling | Question 7
- Java | Exception Handling | Question 8
- Java | Exception Handling | Question 8
- Java | Exception Handling | Question 2
- Comparison of Exception Handling in C++ and Java
- Exception Handling with Method Overriding in Java
- Output of Java program | Set 12(Exception Handling)
- Exception handling in JSP
- Static blocks in Java
- Order of execution of Initialization blocks and Constructors in Java
- Exception Propagation in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



