• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

try catch finally

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
David Brossard
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's fine for checked exceptions. What about RuntimeException and Error, then?!
 
David Brossard
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic