Friday, February 27, 2009

simple example to demonstrate Nested try catch

package exceptions;
// take for example Statement 1,statement 2..... some peace of code .
public class NestedTry {
public static void main (String arg[])
{
try
{
System.out.println("Statement 1");
System.out.println("Statement 2");
System.out.println("Statement 3");
try
{
System.out.println("Statement 4");
System.out.println("Statement 5");
System.out.println("Statement 6");
}
catch(ArithmeticException e)
{
System.out.println("Statement 7");
}
System.out.println("Statement 8");
}
catch(Exception e)
{
System.out.println("Statement 9");
}
System.out.println("Statement 10");

}
}

Explanation:
Case 1:- If there is no Exception ->1, 2, and 3,4,5,6,8,10 is executes. So normal termination.
Case 2:- If an Exception raised at stmt2 and the corresponding catch block is matches then 1,9,10 are executes. So, normal termination.
Case 3:- If an Exception raised at stmt2 and the corresponding catch block not found. 1 fallowed by abnormal termination.
Case 4:- If an Exception raised at stmt5 and the corresponding catch block is matches then 1,2,3,4,7,10 and Normal Termination.
Case 5:- If an Exception raised at stmt5 and the corresponding catch block is not matched but outer catch block has matched 1,2,3,4,9,10 and Normal Termination.
Case 6:- An Exception raised at stmt2 and the corresponding catch block has matched but while executing that catch block another exception raised then fallowed by abnormal termination.
Case 7:- An Exception raised at stmt8 and the corresponding catch block has matched then 1,2,3,9,10 fallowed by Normal Termination. If the catch block has not matched abnormal termination



0 comments:

Post a Comment