At the inner try block, the value of x is 1 therefore 'Level1Exception' will be thrown in case 1. The catch is only for Level2Exception therefore it won't enter there. The 'finally(c++)' will be executed and the value of c = 1 The outer catch(Level1Exception) will catch the above exception and thereby increments the values of d. d =1. Outer finally(g++) makes g=1.
The important thing to remember here Reena is that Level1Exception IS-NOT-A Level2Exception, so the catch block for Level2Exception will not be run. BUT, the finally blocks will always run, no matter what.
If the catch block for Level1Exception was not there, then the catch block for Exception would have been run, as Level1Exception IS-A Exception. And, as before, the finally block always runs.
R .sourav nayak
Joined: May 14, 2006
thanks guys for the reply. Krishnan your explaination was really helpful.
You may find that a useful tool to answer questions like this is to run the code through a debugger such as the ones built into NetBeans or Eclipse. The debuggers are possibly the most userful features of these tools, and for most learning purposes I recommend the naked JDK.