Thursday, January 8, 2009

finally contd.: an interesting try-catch-finally scenario


If not very comfortable with finally block and its execution then you may probably like to go through this article first - playing with try-catch-finally in Java >>. It covers some pretty interesting scenarios covering various possible scenarios of try, catch, and finally blocks followed by the precise explanation of their execution in each of these scenarios. Come back to this article once you finish that as this article is more like a logical extension of that article. If already comfortable with try-catch-finally then you may proceed straightaway with this one.

One of our visitors has asked another interesting scenario involving nested try blocks in response to the above mentioned article. I have created two possible cases taking reference of that scenario and have tried to explain the execution of each of these cases below:

Case #1: nested 'try' with a 'finally' block associated with the inner 'try' and a 'catch' with the outer


public class TestFinally {

public static void main(String[] args) {

try{
try{
throw new NullPointerException ("e1");
}
finally{
System.out.println("e2");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}


Output: what do you expect as the output here? As we all know that 'finally' will always get executed irrespective of 'try' finishes its execution with or without exception. In our case the inner 'try' throws an exception which has no matching 'catch' associated with the corresponding (inner) 'try' block. As per the Exception Mechanism in Java, the uncaught exception will be propagated until it's either executed by some matching 'catch' block on its way back or it'll be handled by the default JVM handler which simply terminates the program after printing a stack trace.

But, before the control leaves the inner 'try' block, the entire inner block should complete its execution and hence the associated 'finally' block will get executed before the control leaves the inner block for the simple reason that 'finally' in any case has to be executed and the control won't go back to the inner block once it comes out of it.

The 'catch' block associated with the outer 'try' block expects an exception of type 'Exception' and hence a 'NullPointerException' (a subclass of the the Exception class) thrown from the inner 'try' block (which has been propagated) can be handled by this 'catch' handler and hence this 'catch' block will be executed. Do I still need to tell you the output of this code-snippet. Here is it for your reference:

e2

java.lang.NullPointerException: e1

at TestFinally.main(TestFinally.java:8)


Case #2: nested 'try' with 'catch' block associated with the inner 'try' and 'finally' with the outer

public class TestFinally2 {

public static void main(String[] args) {

try{
try{
throw new NullPointerException ("e1");
}
catch(Exception e){
e.printStackTrace();
}
}
finally{
System.out.println("e2");
}
}
}


Output: This is pretty straightforward I think. Since an exception is being thrown from the inner 'try' block and we have an associated matching 'catch' handler for this exception so obviously it will be handled there. Now the control leaves the inner block and hence the outer 'finally' gets executed as 'finally' block in any case has to be executed. All right? Please find below the output for your reference:

java.lang.NullPointerException: e1

at TestFinally2.main(TestFinally2.java:8)

e2


Liked the article? Subscribe to this blog for regular updates. You may also like to follow the blog to manage the bookmark easily and to tell the world that you enjoy GeekExplains. Please find the 'Followers' widget in the rightmost sidebar.



Share/Save/Bookmark


2 comments:

Avenue 2 said...

Thanks for the great explanation. Sorry I missed the closing brace.

Anonymous said...

thank You for all of Your post, they are very helpful to me
I'm prepering for scjp and this deeper insight gives me better understanding whats going on
I've read like 10 post already
well written, just the right amount of text and examples