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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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
}

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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");
        }
    }
}

chevron_right


Output:

ArrayIndexOutOfBoundsException main try-block


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.