exceptions in finally

  • Thread starter Thread starter Sankar Nemani
  • Start date Start date
S

Sankar Nemani

Hi all,
If there is an exception inside the finally part of the try block, then
the original exception will be lost. Other than making sure the finally code
does not throw any exceptions and/or putting a try-catch (and ignore
exceptions) block around the code in finally, is there any better way to
handle this situation?
TIA
Sankar Nemani
 
Hi Sankar,

Thanks for posting in this group.
Based on my understanding, you wanna a way to handle the exception
generated in finally block.
Other that using try..catch block in finally, you can include all your work
code(Including your finally block code) in a "big" try...catch block, which
handles all the unhandled common exceptions.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Sankar,

Do you still have any concern?
If you have anything unclear, please feel free to tell me.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Actually I do. Even if I have a big try-catch block, I still may need some
clean-up code to use, which instead of repeating in both try and catch, I
could put inside finally. But what if there is an exception in the code in
finally? Is it a common practice to have a try catch there again or just let
the original exception go and just report the new exception?
 
Personally, I catch the new exception and log it, but propagate the original
exception. You propagate the original exception by not throwing a new one.
For example:

try {

// do something cool

} catch( Exception e ) {
logger.Error( "you failed me", e );
throw e;
} finally {
try {
// cleanup
} catch( Exception e ) {
logger.Warn( "uh oh, exception during cleanup", e );
}
}

- jeremiah

Sankar Nemani said:
Actually I do. Even if I have a big try-catch block, I still may need some
clean-up code to use, which instead of repeating in both try and catch, I
could put inside finally. But what if there is an exception in the code in
finally? Is it a common practice to have a try catch there again or just let
the original exception go and just report the new exception?

"Jeffrey Tan[MSFT]" said:
Hi Sankar,

Do you still have any concern?
If you have anything unclear, please feel free to tell me.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Sankar,

Thanks for your feedback.
My reply of using "big" try...catch block to handle all the unhandled
common exception supposed that all the clean up work has been done well in
inner finally block.
Also, you can follow other people's suggestion to use try...catch in
finally block, but even in this try...catch block, I think you may need
clean up work again, so the handle is endless....
Actually, finally block is only for you to do some relative "safe" clean up
code, which seldom generate exception, so exception in the finally block is
not the main impact.

For your exception in finally block, I think you can determine your handle
logic yourself, maybe you can just throw a self-defined exception, or you
can just write into the eventlog a clean up fail message, etc..

Hope my reply makse sense to you. If you have further concern, welcome to
reply to me, I will help you more.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top