| Author |
not able to understand the output of the following piece of code ?
|
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 867
|
|
when i ran this code it gives me null pointer exception followed by 0. i know that i points to null and when we did System.out.println(i.length) it gives runtime exception . but according to me since the nested try block does not have any associated catch block , so when an exception is throw in it , the control will go inside finally block , which assigns reference of eye to i . but i know that catch block runs which prints the output . i want to know is that how come catch block gets executed since inner try block does not have any associated catch block with it. please explain
|
OCPJP 6(100 %) OCEWCD 6(91 %)
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
|
The flow would be Inner try block -> Inner finally block -> Outer catch block for any Exception should occur and no catch block is present for the inner try block.
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
|
The presence of the finally block doesn't stop the exception from propagating out to the enclosing try/catch. It just means that before that happens, the code inside the finally block will execute. If the inner try block were accompanied by a catch, then the exception wouldn't propagate outwards.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
John Jai wrote:The flow would be Inner try block -> Inner finally block -> Outer catch block for
unless there is an exception immediately after the Outer try else, inner finally block will be skipped
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
Seetharaman Venkatasamy wrote:
John Jai wrote:The flow would be Inner try block -> Inner finally block -> Outer catch block for
unless there is an exception immediately after the Outer try else, inner finally block will be skipped
Yes - when the inner try din't even get executed.
|
 |
 |
|
|
subject: not able to understand the output of the following piece of code ?
|
|
|