If not very comfortable with finally block and its execution then you may probably like to go through this article 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
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:
Case #2: nested 'try' with 'catch' block associated with the inner 'try' and 'finally' with the outer
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:
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
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.