try-catch
We can handle exceptions by using the fallowing keywords.
1). try 2). catch 3). finally
try
{
//Risky code
}
catch(XException e)
{
//Define the handler
}
Example:
try
{
System.out.println(10/0);
}
catch(ArithematicException e)
{
System.out.println(“caught”);
System.out.println(10/2);
}
System.out.println(“hello”);
output:
caught
5
hello
Example: try {
Statement 1;
Stmt 2;
Stmt 3;
}
catch(XException e){ Statement 4; }
stmt5;
See the below cases:
Case 1:- If there is no Exception ->1,2,3,5 are executes. So normal termination.
Case 2:- If an Exception raised at stmt2 and the corresponding catch block found then 1 fallowed 4,5 are executes. So, normal termination.
Case 3:- If an Exception is raised at stmt2, but the corresponding catch block not found. Then stmt1 fallowed by abnormal termination.
Case 4:- If an Exception raised at stmt2 and the corresponding catch block found; but while executing catch block raises another Execution then stmt1 fallowed by abnormal termination.
Methods For Displaying Error Information :
The class Throwable contains the fallowing methods for displaying Error information.
1. printStackTrace():
Name of the Exception: Description of Exception Stack trace.
catch(ArithmaticException e)
{
e.printStackTrace();
}
System.out.println(“hai..”);
output:
java.lang.ArithmaticException: /by zero at sample.main(Sample.java)
Hai
2. public String toString():
Name of the Exception: Description of Exception
catch(ArithmaticException e)
{
System.out.println(e.toString());
}
System.out.println(“hai..”);
output:
java.lang.ArithmaticException: /by zero
Hai
3. public String getMessage():
Description of Exception
catch(ArithmaticException e)
{
System.out.println(e.getMessage());
}
System.out.println(“hai..”);
output: /by zero
hai
Try with Multiple catch blocks:
• The way of handling the exception is varied from exception to exception, hence it is a good programming practice to keep separate catch block for every exception.
Example:
try
{
System.out.println(10/0);
}
catch(ArithmeticException e) //child
{
//code
}
catch(Exception e) //parent
{
//code
}
Here in the above example ArithmeticException is the child class of Exception class.It is Valid since child --> to ---> parent hierarchy.
Example:
try
{
System.out.println(10/0);
}
catch(Exception e) //parent
{
//code
}
catch(ArithmeticException e) //child
{
//code
}
Here in the above example Exception class is the child class of ArithmeticException class.It is InValid since parent -->to ---> child hierarchy.compile time error(CTE) : Exception has already been caught by catch (ArithmeticException e).
• If multiple catch blocks are defined for the try statement, the order of the catch blocks is important. We should take the catch blocks from child to parent, violation leads to CTE saying “Exception” has already been caught”.
Control Flow in try with multiple catch blocks:
try
{
Statement 1;
Stmt2;
Stmt3;
}
catch(XException e)
{
Stmt 4;
}
catch(YException e1)
{
Stmt 5;
}
Stmt6;
see the below cases:
Case 1:- If there is no Exception ->1,2,3,6 is executes. So normal termination.
Case 2:- If an Exception raised at stmt2 and the corresponding catch block(Y Exception e1) is matches then 1,5,6 are executes. So, normal termination.
Case 3:- If an Exception is raised at stmt2, but the corresponding catch block not found. Then stmt1 fallowed by abnormal termination.
Control Flow in Nested try catch :
try
{
Statement 1;
Stmt2;
Stmt3;
try
{
Stmt4;
Stmt5;
Stmt6;
}
catch(XException e)
{
Stmt7;
}
Stmt8;
}
catch(YException e)
{
Stmt9;
}
Stmt10;
See the below cases:
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