File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Return from finally block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Return from finally block" Watch "Return from finally block" New topic

Return from finally block

tanu dua
Ranch Hand

Joined: Apr 05, 2004
Posts: 145
Hi all !

Output : Prints nothing.

Problem : I think the output should be :
An exception is thrown and the exception message thrown from first catch is diplayed with the exception stack trace,because of the following reasoning:

When finally block is returning ,it is returning to the JVM which i think is the default Exception Handler,so stack trace should be printed.

Please sort out this..

[ August 24, 2004: Message edited by: Barry Gaunt ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
It's important to remember that the finally block will always be executed, regardless of whether or not an exception was thrown and/or caught. The outcome of the finally block may very well dictate what happens to the first exception (if one was thrown).

For example, if your try block ends abruptly and causes an exception to be thrown, your finally block will execute. If your finally block ends abruptly, due to an exception, the second exception (the one from the finally block) will be thrown and the original exception will be lost.

In your case, you're seeing a similar case. The try block ends abruptly and execution flows through the catch block and into the finally block. The finally block, however, tells the method to return, which causes completion of the method before the exception can be propogated back up to the calling method. In such a case, the exception is basically ignored.

I hope that helps,

SCJP Tipline, etc.
I agree. Here's the link:
subject: Return from finally block
It's not a secret anymore!