Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic