Java-Nested Try Statement(Another Example)

Nested Try Statement(Another Example)

Nesting of try statements can occur in less obvious ways when method calls are involved. For example, you can enclose a call to a method within a try block. Inside that method is another try statement. In this case, the try within the method is still nested inside the outer try block, which calls the method.

Program

2

Program Source

class Javaapp {
    
    static void nesttry()
    {
        try{
            int data[] = new int[5];
            data[6] = 200;
                
        }catch(ArrayIndexOutOfBoundsException ar)
        {
            System.out.println(ar);
        }        
    }
    
    public static void main(String args[]) {
        
        try {
            nesttry();
            int value = 200/0;
            
        }catch(ArithmeticException ae) {
           
            System.out.println(ae);
        }
    }
}

Leave a Comment