This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes not able to understand the output of the following piece of code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "not able to understand the output of the following piece of code ?" Watch "not able to understand the output of the following piece of code ?" New topic
Author

not able to understand the output of the following piece of code ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1



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
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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: 1776
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: not able to understand the output of the following piece of code ?
 
Similar Threads
Finally abrupt return
return in try/catch block
Try/catch/finally
try, catch,finally
static block and static variable?