• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error in finally

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The good old Database example...

[ August 19, 2002: Message edited by: Vin Kris ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Meg Adal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another big gotcha is having a return statement inside a finally block. This results in silently catching all exceptions and errors.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic