Search code examples
c#.netcil

In .NET, is finally equivalent to try-catch-throw?


I am writing a static analysis tool for CIL. Control flow analysis would be simplified if finally blocks could be interpreted as try-catch blocks with a rethrow inside the catch. In C#, I fail to see the difference between

try
{
    // ...
}
finally
{
    // finally code here
}

and

try
{
    // ...
}
catch
{
    // finally code here
    throw;
}

or between

try
{
    // ...
}
catch(Exception e)
{
    // catch code here
}
finally
{
    // finally code here
}

and

try
{
    try
    {
        // ...
    }
    catch (Exception e)
    {
        // catch code here
    }
}
catch
{
    // finally code here
    throw;
}

There are even a finally block and endfinally instructions in the CIL. There must be a difference, is there?


Solution

  • No - a finally block is executed even if no exception is thrown, and also even if another catch block catches the exception. (That's true whether the catch block then throws an exception itself or not.)

    Oh, and a finally block will also be executed if the try block returns from the method.

    Basically, if you want code to always execute when execution leaves the statement, finally is what you want. Although in C# I find I rarely write an explicit finally block - a using statement nearly always makes the code simpler.