aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes error in finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "error in finally" Watch "error in finally" New topic
Author

error in finally

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi there:
try {
gaurded code
}
finally {
will be executed -code- no matter what...
}
Java language stresses that system will gracefully abort because finally will always
run. But what if there is an error in finally code?
Thanks
Barkat
[ August 19, 2002: Message edited by: Barkat Mardhani ]
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Hi Barkat,
In that case, the method throws an exception/error, just like in code without try/catch blocks. You could put another try/catch block inside the finally block if you want to handle the exception.
Venkatesh Gowri
Greenhorn

Joined: Aug 15, 2002
Posts: 12
hi
If so then the Runtime system would throw a new exception.It treats the Code in the Finally block just as any other piece of code.


Gowri Venkatesh
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
The good old Database example...

[ August 19, 2002: Message edited by: Vin Kris ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
To sum up, if there is some code in your finally block that throws:
- an Exception (or a subclass thereof) and the code is not wrapped in another try-catch block, the code won't compile.
- a RuntimeException (or a subclass thereof), then the code compiles, but a RuntimeException may be thrown at runtime
- an Error (or a subclass thereof), then the code compiles but an Error may be thrown at runtime and the application may stop.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Meg Adal
Ranch Hand

Joined: Mar 12, 2002
Posts: 70
One more thing to add,
If try block throws RunTimeException (catch block is not catching that Exception)
and finally block also throws RunTimeException then RunTimeException in finally block will be thrown not the one in try block
Please correct me If I am wrong
Megan
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Right Megan
For more detailed information about the behavior of try-catch-finally, please refer to JLS 14.19.2 Execution of try-catch-finally
Larry Lecomte
Ranch Hand

Joined: Jun 14, 2002
Posts: 37
If I understand, the question is if there is a thrown exception in the finally clause does that
cancel the exception that would have been thrown outside of the try block ?
I think that it does, because in the JLS section 14.17 it states:
If there are any enclosing try statements (�14.19) whose try blocks contain
the throw statement, then any finally clauses of those try statements are exe-cuted
as control is transferred outward, until the thrown value is caught. Note that
abrupt completion of a finally clause can disrupt the transfer of control initiated
by a throw statement.
In my opinion, a throw in the finally block disrupts control...But you'll have to do a little sample, to be sure of this
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Another big gotcha is having a return statement inside a finally block. This results in silently catching all exceptions and errors.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error in finally