Wednesday, February 11, 2009

try , catch , finally

finally block:
try
{
System.out.println(10/0);
}
catch(AE e)
{
System.out.println(“catch”);
}
finally
{
System.out.println(“catch”);
}
• The cleanup code, we have to keep inside finally block because irrespective of exception occurred or not occurred or handled or not handled, the finally block will always execute.

Q: Difference between final, finally and finalize?
• It is highly recommended to keep cleanup code inside finally block instead of finalize().
• The finally block will always execute except the place where the JVM got shutdown. We can shutdown the JVM by calling /using System.exit(0).

Example:
try

{
System.out.println(“hai”);
}
catch( NULL pointer exception e)
{
System.out.println(“catch”);
}
finally
{
System.out.println(“finally”);
}
output:
hai
finally

Samples:
1. try{} catch(X e){} -->valid

2. try{}catch(X e){}finally{} -->valid
3. try{}finally{}catch(X e){} --> invalid
4. try{}finally{} -->valid
i.e , try --> catch --> finally is the only order.
Only try{} ---> invalid,
Only catch{} ----> invalid
Only finally{} ----> invalid
Note: try without catch & catch with out try invalid

Example:
catch(..){}
finally{}
CTE: catch with out try

Control Flow in try, catch, finally block :
try
{
Stmt1;
Stmt2;
Stmt3;
}
catch(XException e)
{
Stmt 4;
}
finally
{
Stmt 5;
}
Stmt 6;

See the below cases:

Case 1:- If there is no exception then 1,2,3,5,6 and normal termination.
Case 2:- If an exception raised at stmt2 and the corresponding catch block found 1,4,5,6 and normal termination.
Case 3:- If an exception raised at stmt2 and the corresponding catch block not matched 1,5 and abnormal termination.

Control Floe in the Nested try, catch, finally blocks:
try
{
Stmt1;
Stmt2;
Stmt3;
try
{
Stmt4;
Stmt5;
Stmt6;
}catch(XException e)
{
Stmt 7;
}
finally
{
Stmt 8;
}
Stmt 9;
}
catch(YException e)
{
Stmt 10;
}
finally
{
Stmt 11;
}
Stmt 12;

Case 1: If there is no exception 1,2,3,4,5,6,8,9,11,12 and normal termination.
Case 2: If an exception raised at stmt 2 and the corresponding catch block found then 1,10,11,12 and normal termination.
Case 3: If an exception raised at stmt2 and the corresponding catch block has not matched then 1,11 and abnormal termination.
Case 4: If an exception raised at stmt5 and the corresponding catch block has matched then 1,2,3,4,7,8,9,11,12 and normal termination.
Case 5: If an exception raised at stmt5 and the corresponding catch block has not matched but the outer catch has matched then 1,2,3,4,8,10,11,12 and normal termination.
Case 6: If an exception raised at stmt5 but the inner and outer catch blocks are not matched then 1,2,3,4,8,11 and abnormal termination.

‘Throw’ keyword:
public static void main(String a[])
{
try
{
Throw new AithmeticException()
}
}
By using ‘throw ‘ keyword, over created customized exception objects are handed over to JVM.

Example:
class Sample
{
public static void main (String a[])
{
throw new ArithmeticException();
System.out.println(“hai”);
}// CTE: Unreachable statement
}
After throw we are not allowed to keep any statement, violation leads to CTE saying “unreachable statement”.

Example:
class Sample
{
static ArithmeticException e;
public static void main(String[] a)
{
throw e; //invalid
}
}
output: NullPointerException
‘e’(is in instance area) is not pointing “ArithmeticException’ here.
static ArithmeticException e=new ArithmeticException();

Example:
class Sample
{
public static void main(String a[])
{
Throw new Exception(); //invalid
}
}//CTE: “Unreported Exception Java.lang.Exception must be caught” or declared to be thrown.
• Inside a program if there is any chance of throwing a checked exception explicitly or implicitly we have to handle that checked exception by using try - catch or we have to delegate the responsibility or exception handling to the caller. Violation leads to CTE saying Unreported Exception xxx; must be caught or declared to be thrown.

0 comments:

Post a Comment