Login    New User    Forgot Password    Help      

Nested Try

Java Tutorials

Example

// Nested try statement
// Multiple catch clauses
// Run with argument 0,1,2;

class ex
{
int m;
        void div(int n)
        {
       
        // monitor block of code
        try
                {
                n--;
                m=m/n;
                // this will not print on the time of divide by 0
                System.out.println("Divide  "+ ++n +" by " + --n +" == " + m);

        
         //nested try block
         try
                {
                int c[]={1};
                c[1]=99;
                }
               
         catch (ArrayIndexOutOfBoundsException e)
                {
                System.out.println("Array index obb : ");
                }
          }
         catch (ArithmeticException e)
                {
                System.out.println("Divide by zero is Not possible");
                // use following statement to see the error
                //System.out.println(e);
                }
                }
}
 
class  ex3
{
public static void main(String args[])
{
int n;

n=Integer.parseInt(args[0]);
ex ob1=new ex();
ob1.div(n);

}
}

Output

Java Tutorials