wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Try - catch & finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Try - catch & finally" Watch "Try - catch & finally" New topic
Author

Try - catch & finally

Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
Hi,
Well, now I'm confused: I thought a finally block will always be executed, no matter what code is in the catch block. Am I wrong??
well, I had to find out that System.exit(0) in the catch block exits the application without executing the finally block. (which kinda makes sense).
But what about a return statement in the finally block, will that be executed or not?
Thanks alot.
Leonardo FIgueiredo
Greenhorn

Joined: Apr 05, 2004
Posts: 2
If you tell your program to catch an error and exit, why should it return something? You are out, gone, finito.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Originally posted by Ben Buchli:
But what about a return statement in the finally block, will that be executed or not?

Yes, it'll still be executed. As you'd found out, the only time when the codes in finally block aren't executed are when you call System.exit();


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
several circmstances that can prevent the execution of finally block
1. An Exception arising in the finally block without try/catch for
handling that exception.
2. the death of the thread.
3. use of System.exit().
4. power to CPU off.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Quoting The Java Preogramming Language, Third Edition, page 206:
A finally clause is always entered with a reason. The reason may be that the try code finished normally, that it executed a ... return, or that an exception was thrown ... The reason is remembered when the finally clause exits by falling out the bottom. However, if the finally block creates its own reason to leave by executing a ... return, ... that reason supercedes the original one and the original reason is forgotten.
In short, a return statement in a finally block is acted on immediately on execution.
I was not able to find what happens if the return statement is in the catch block. Maybe I'll try it out and see.


Mike Gershman
SCJP 1.4, SCWCD in process
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch, Ben!
But what about a return statement in the finally block, will that be executed or not?
Note that a very good answer to this question (and others of its type) could be discovered by simply writing a small test program and observing what happens.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
[Going a bit off-topic]
As an old system programmer type, I would warn that if a language question is not answered in the definitive documents, there is a risk in trying it out and relying on the result. You may wind up with an implementation-dependent program.
If there is no clear language rule on the action of a return statement in a catch block, the actual result may be the unintended consequence of a particular compiler or jvm design. A future release or different product may give a different result. If this configuration is not in the test suite, the difference will only show up in your program.
Of course, if lots of programmers rely on it, the difference will show up in beta testing. Whether that is remedied depends mostly on how much fuss is raised and how hard it is to conform to the older code.
My favorite example is the wide use of undefined instructions on the old IBM 1401. When the IBM 360/30 came out with a 1401 emulator, IBM was forced to implement the undefined instructions. They even published a manual documenting these instructions.
But it is still not best practice to rely on the undocumented behavior of a product.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try - catch & finally
 
Similar Threads
Try, Catch, Finally
execution of finally
Unreachable statement
Need for finally block?