File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finally block executing before catch PRoblem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i think answer should be:

Inside fnA
Exception Caught
fnA's finally
Inside fnB
fnB's finally

but the correct answer is:


Inside fnA
fnA's finally
Exception Caught
Inside fnB
fnB's finally
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:

i think answer should be:

Inside fnA
Exception Caught
fnA's finally
Inside fnB
fnB's finally



Why do you think so ? The method fnA will complete and then execution of code in main method occurs.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The finally block in fnA executes before that method is exited. The exception can't be caught until after the method is exited, so the given answer is correct.
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally block inside a method should execute before the method returns or throws an exception.

A most common use of finally block is to close the jdbc connection object.

for example:

finally{
conn.close();
}

where conn is the reference to the connection object.

But now imagin what will happen if method returned before finally got executed. --> There will be no conn reference available to finally block because the local stack variables for the method would be gone.

So it is quite logical to think that finally block should be executed before method returns or throws exception.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didn't get it.
why is the finally block executing after catch?
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:i didn't get it.
why is the finally block executing after catch?

Because the catch is in a completely different method. Once you're outside fnA, you're outside it. The finally block will be the last thing that happens inside fnA, but the catch doesn't happen till you've left it.
 
joy b chakravarty
Ranch Hand
Posts: 62
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On similar lines...


what do you think gets printed here.
Answer> Value : 2.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic