Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception handling: Is finally executed after throw?

Assume you have the following code:

Instead of doing:

Try
    '
    ' Initialize some objects
    '

    '
    ' do something that fails
    '

    '
    ' Clean up-code that gets not reached because exception
    '
Catch e As Exception
    '
    'Clean up initialized objects
    '

    Throw e
End Try

I would like to do:

Try
    '
    ' Initialize some objects
    '

    '
    ' do something that fails
    '
Catch e As Exception
    Throw e
Finally
    '
    'Clean up initialized objects
    '
End Try

So my simple question is: In case of an exception is the finally block reached even if there is a throw some lines before?

[EDIT] Thanks for your fast answers.

In first line there will be NullReference-, COM- and FileNotFound-Exceptions I think.

Ok, I will go for this code:

Try
    '
    ' Initialize some objects
    '

    '
    ' do something that fails
    '
Catch e As Exception      ' or just "Catch"??        
    Throw
Finally
    '
    'Clean up initialized objects
    '
End Try

All the best!

Inno

like image 230
Inno Avatar asked Oct 16 '09 09:10

Inno


People also ask

Does finally execute after throwing exception?

A finally block always executes, regardless of whether an exception is thrown.

Is finally used in exception handling?

The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the connection. The finally block executes whether exception rise or not and whether exception handled or not. A finally contains all the crucial statements regardless of the exception occurs or not.

What happens if finally throws an exception?

The "finally" block execution stops at the point where the exception is thrown. Irrespective of whether there is an exception or not "finally" block is guaranteed to execute. Then the original exception that occurred in the try block is lost.

Does try finally throw exception?

Yes, it absolutely will. Assuming your finally block doesn't throw an exception, of course, in which case that will effectively "replace" the one that was originally thrown.


2 Answers

So my simple question is: In case of an exception is the finally block reached even if there is a throw some lines before?

Yes. The Finally block is always1) executed and exists precisely for clean-up. In your code, remove the Catch block, it does nothing. Worse, it actually destroys the stack trace because you don’t re-throw the original exception, you throw a new one.

If you really need a Catch block that then re-throws the exception, use the following:

Catch e As XyzException
    ' … do some stuff. '
    Throw
End Try

1): Caveat emptor: there are some exceptions such as StackOverflowException (how fitting …) which require special attention and may not trigger the Finally block. Handling them correctly is usually quite difficult.

like image 173
Konrad Rudolph Avatar answered Oct 12 '22 01:10

Konrad Rudolph


No, it is NOT guaranteed to run. There are certain exceptions - for example StockOverflowException and OutOfMemoryException - where the execution of a finally block is not guaranteed.

like image 40
Maximilian Mayerl Avatar answered Oct 12 '22 00:10

Maximilian Mayerl