aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "try catch finally" Watch "try catch finally" New topic
Author

try catch finally

David Brossard
Ranch Hand

Joined: Jun 03, 2004
Posts: 109
Hi,

When I have a try catch finally statement, the point of finally is to ensure whatever code it contains will be executed.

But what if I have an exception thrown in the catch:



Of course having written the code I tried it out. The result is that finally always gets executed:


[ September 14, 2006: Message edited by: David Brossard ]

No matter what they say in Ohio, we're still first in flight!
Anca Sarbu
Greenhorn

Joined: Sep 10, 2006
Posts: 2
Hello,

What you just discoverd about try-catch-finally statement is right, finally is allaws executed, no mather what happens in the catch clause.

Read more about this in The Java Language Specification, 14.20.2 Execution of try-catch-finally :

Blocks and statements

Anca.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Yes, you just discovered what "finally" is often used for: ensuring some code executes, even if the code above threw.

An aside: what do you think happens if the code in "finally" throws, while processing an exception from the "try" block?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
David Brossard
Ranch Hand

Joined: Jun 03, 2004
Posts: 109
The exception thrown in the finally is out of the scope of that finally. Therefore the finally doesn't apply anymore and if there is any code within that finally after the exception is thrown, it won't be reached.

Eclipse actually warns you twice:

a. the finally block doesn't end normally
b. there is unreachable code

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
That's fine for checked exceptions. What about RuntimeException and Error, then?!
David Brossard
Ranch Hand

Joined: Jun 03, 2004
Posts: 109
In the case of a runtime exception e.g. a conversion error, the finally is executed.

Example code and result:



Result
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by David Brossard:
In the case of a runtime exception e.g. a conversion error, the finally is executed.


No, I meant a RuntimeException or Error occurring during processing of the "finally". In that case, the compiler cannot help you, because any method call might throw a RuntimeException or Error.

As far as I know, a RuntimeException or Error occurring during processing of the "finally" will abort the "finally", forget any exception that was thrown in the "try" and pass the new exception/error to the caller. Nasty.

Of course, all Errors and most RuntimeExceptions are serious problems that ought not to be happening, except in the presence of bugs or serious configuration problems. So this issue doesn't cause problems often, in practice.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Peter Chase:


No, I meant a RuntimeException or Error occurring during processing of the "finally". In that case, the compiler cannot help you, because any method call might throw a RuntimeException or Error.

As far as I know, a RuntimeException or Error occurring during processing of the "finally" will abort the "finally", forget any exception that was thrown in the "try" and pass the new exception/error to the caller. Nasty.

Of course, all Errors and most RuntimeExceptions are serious problems that ought not to be happening, except in the presence of bugs or serious configuration problems. So this issue doesn't cause problems often, in practice.


Yes, you're screwed.
If a Throwable is thrown (or you explicitly return) from a finally block, your code is defective. If this holds, then there is a contradiction here (reductio ad absurdum): Sun (and therefore, the Java community) states that any piece of code may throw an exception - which is why we have "unchecked exceptions" for "developer errors", therefore, since throwing an exception within a finally block is defective, a "finally block" and "a piece of code" must not co-exist. As a result, the only legitimate finally block is this:
finally{} which is absurd, therefore, the only finally block is no finally block.

Conclusion: If it holds that throwing or returning from a finally is defective and it also holds that "unchecked exceptions are for unforeseen developer errors", then there is no such thing as a legitimate finally block. I propose that "unchecked exceptions are for unforeseen developer errors" does not hold.


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try catch finally