Implement Try - Catch block using Java



Write a program in Java to implement Exception
handling using nested try catch block





Program:


class Sk25


 {


  public static void main(String args[ ])


   {


    int
a=60,b;


    for (int i=2;i>=0;i--)


{


try{


     b=a/i;


System.out.println("Output:"+b);


for (int j=2;j>=0;j--)


 {


  try{


      b=a/j;


           System.out.println("Output:"+b);


     }


  catch(ArithmeticException e){


      System.out.println("Inner Catch: Divide
By Zero");


                              }


 }


}


catch(ArithmeticException
e){


  System.out.println("Outer Catch: Divide
By Zero");


                            }


     }


    }


}        


Compile:


D:\>javac Sk25.java


Run:


D:\>java Sk25


Output:


Output:30


Output:30


Output:60


Inner Catch: Divide
By Zero


Output:60


Output:30


Output:60


Inner Catch: Divide
By Zero




Outer Catch: Divide
By Zero


Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C