Open In App

Nested try blocks in Exception Handling in Java

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 onto a stack. Given below is an example of a nested try. In this example, the 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 to Demonstrate Nested Try Blocks in Exception Handling in Java

Below is the implementation of Nested Try Blocks in Exception Handling in Java:

Java

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

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.

Why “division by zero is not possible” not printed on the Screen?

Here is the “division by zero is not possible” statement is executed, but it is done within the inner catch block, not in the outer catch block for ArrayIndexOutOfBoundsException. If you want to see both messages, you can modify the code to print the “division by zero is not possible” message outside the inner try block.

Example 2:

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

Output
ArrayIndexOutOfBoundsException main try-block



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads