JAVA: Nested try-catch Blocks

Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested, one within another. In java, this can be done usingnested try blocks. A try statement can be inside the block of another try. In a nested try block, every time a try block is entered the context of that exception is pushed on the stack.

The following code snippet explains the syntax of a nested try…catch block.

....
try
{
    statement 1;
    statement 2;
    try
    {
        statement 1;
        statement 2;
    }
    catch(Exception e)
    {
    }
}
catch(Exception e)
{
}
....

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 until all nested try statements are exhausted. If no matching blocks are found, the Java Runtime Environment handles the execution.

The following example illustrates how to use nested try blocks

class NestedTry
{
    public static void main(String args[])
    {
        try
        {
            int num = args.length;
            try
            {
                int numValue = Integer.parseInt(args[0]);
                System.out.println("The square is " + numValue * numValue);
            }
        catch(NumberFormatException nb)
        {
            System.out.println("Not a number! ");
        }
    }
        catch(ArrayIndexOutOfBoundsException ne)
        {
            System.out.println("No arguments given! ");
        }
    }
}

The output of the program is:

When try blocks are used without any catch blocks, the program complies without without any problems, but terminates, but terminates during run time. This is because the exception is thrown at run time.

Nesting of try statements can also occur when method calls are involved. For example, a method call can be enclosed within a try statement. Inside this method there could be another try statement. In such a case, the method’s try block is still nested within the outer try block where the method call is present.

The following example illustrates shows nested try block in a method:

class TryMethods
{
    static void methtry(int val)
    {
        try
        {
            if (val == 1)
            val = val / (val - val);
            if (val == 2)
            {
                int num[] = {42};
                num [5] = 99;
            }
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("Trying to access an array element out of range and hence \n" + e);
        }
    }
    public static void main(String args[])
    {
        try
        {
            int num = args.length;
            int num1 = 42 / num;
            System.out.println("num = " +num);
            methtry(num);
        }
        catch(ArithmeticException e)
        {
            System.out.println("Division by zero is not possible" +e);
        }
    }
}

The output of the code is: