Skip to content

.NET 10 changes the way nested try-finally are handled #121578

@Scooletz

Description

@Scooletz

Description

It looks like .NET10.0 changes the way nested try-finally blocks are handled. This might be a breaking change for nested using statements that might throw some exception. in .NET8. Yes, one should not throw from finally blocks, but still, it may happen especially with the nested usings.

Reproduction Steps

If you create a simple Program.cs with the following setup and run it under .NET 8.0 it runs properly. Under .NET 10.0 it skips the internal catch/ finally` throwing the nested exception.

bool internalFinally, externalFinally, internalCatch;

try
{
    throw new Exception("BOOM");
}
catch (Exception e)
{
    try
    {
        try
        {
        }
        finally
        {
            throw new Exception();
        }
    }
    catch(Exception)
    {
        // Swallow
        internalCatch = true;
    }
    finally
    {
        internalFinally = true;
    }
}
finally
{
    externalFinally = true;
}

Console.WriteLine(string.Join(", ",
    $"{nameof(internalCatch)}: {internalCatch}",
    $"{nameof(internalFinally)}: {internalFinally}",
    $"{nameof(externalFinally)}: {externalFinally}"
));

Expected behavior

Execute the catch and finally as it was under NET8.0. The result was

internalCatch: True, internalFinally: True, externalFinally: True

Actual behavior

The catch-finally is omitted altogether resulting in a breaking behavior. Resulting in

Unhandled exception. System.Exception: Exception of type 'System.Exception' was thrown.

Regression?

Yes.

Known Workarounds

No response

Configuration

.NET10.0 and .NET8.0
Windows
x64

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions