This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enthuware questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enthuware questions" Watch "Enthuware questions" New topic

Enthuware questions

k reeta
Ranch Hand

Joined: Oct 15, 2010
Posts: 35
From what I have understood if a risky method in a try block throws an exception, the rest of the try block is not executed. Instead, the catch/finally blocks are executed. But what about the rest of the code in the same method as the try block.

The output of the above code is 1 4 5, which implies that the code after "finally" is executed.

BUT here,

The output is 13423, which implies that the code after finally is not executed.

What am I missing?

Sanjeev Ba
Ranch Hand

Joined: Dec 31, 2006
Posts: 40

From Kathy Sierra book.

A finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not.
Even if there is a return statement in the try block, the finally block executes right after the return statement is encountered, and before the return executes!

Based on the code sample provided, I think the behaviour is right.

Hit if(i==2) -> throw exception, handle exception print "2" and return, but just before returning, execute finally -> print 3.

Hope this clarifies.
k reeta
Ranch Hand

Joined: Oct 15, 2010
Posts: 35
I completely missed the return statement! Makes sense now, thank you!
wood burning stoves
subject: Enthuware questions
Similar Threads
flow control
exception handling
Why is 5 getting printed here?