runtime: Change in exception handling in 9.0

Description

If you have a try-catch-catch, I don’t believe C# allows the second catch to handle exceptions thrown by the first catch. At least, that’s the behavior I’m seeing in 8.0. In 9.0, there seem to be cases where the second catch can handle them.

Reproduction Steps

try
{
    Console.WriteLine("Try");
    throw new NotSupportedException();
}
catch (NotSupportedException)
{
    Console.WriteLine("NotSupportedException");
    throw new Exception("Repro failed");
}
catch (InvalidOperationException)
{
    Console.WriteLine("InvalidOperationException");
    try
    {
        System.Diagnostics.Debug.Fail("How did we get here?");
    }
    finally // Required for repro
    {
        System.Diagnostics.Debug.Fail("How did we get here?");
    }
}
catch (Exception)
{
    Console.WriteLine("Exception");
    Console.WriteLine("Repro succeeded");
}
finally
{
    Console.WriteLine("Finally");
}

Expected behavior

8.0 printed

Try
NotSupportedException
Unhandled exception. System.Exception: Repro failed [SNIP]
Finally

Actual behavior

9.0.100-preview.5.24229.2 prints

Try
NotSupportedException
Exception
Repro succeeded
Finally

Regression?

No response

Known Workarounds

No response

Configuration

9.0.100-preview.5.24229.2 (in aspnetcore repo) Win11 22631.3447 on x64 (haven’t tried others)

Other information

The nested try finally in the unreachable catch block seems essential to the repro.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 24 (24 by maintainers)

Commits related to this issue

Most upvoted comments

FYI the ASP.NET Core tests now pass.

Just to confirm nothing relevant on the JIT side has changed, I can reproduce this issue on .NET 8 when targeting NativeAOT in Debug – the ordering of the EH clauses is the same as above.

@janvorli how is isSameTry computed? I’m assuming when the runtime searches for an exception handler, it iterates in the order the handlers are placed in memory, right?

I am sorry for a late response. I have written my response here few days ago, but apparently I must have forgotten to push the comment button. The isSameTry is the flag CORINFO_EH_CLAUSE_SAMETRY that JIT sets here: https://github.com/dotnet/runtime/blob/189d768ee55a32a102a01ea8633ecb44c057863e/src/coreclr/jit/codegencommon.cpp#L2404-L2416 It needs to be set correctly too in order to make things work.

I have investigated it. It turns out that NativeAOT shares the same issue when compiled for Debug. Release build doesn’t have this issue even in coreclr. The issue is caused by the JIT ordering of exception clauses. In this specific case, the clause for the finally marked by the “// Required for repro” comment is placed in between the clauses of the main try block. When the new EH and the NativeAOT EH unwinds out of the “catch”, the unwinder moves to the frame of the first throw. It then wants to skip all clauses that belong to the same try block as the catch clause that was invoked to handle the first exception. But the inserted clause from the other try block causes it to believe there are no more clauses for the same try. Here are the clauses from a debug build as I have printed them using an added logging:

  EH clause to consider curIdx=0, _tryStartOffset=2F, _tryEndOffset=75, isSameTry=False, clauseKind=RH_EH_CLAUSE_TYPED
  EH clause to consider curIdx=1, _tryStartOffset=152, _tryEndOffset=166, isSameTry=False, clauseKind=RH_EH_CLAUSE_FAULT
  EH clause to consider curIdx=2, _tryStartOffset=2F, _tryEndOffset=75, isSameTry=False, clauseKind=RH_EH_CLAUSE_TYPED
  EH clause to consider curIdx=3, _tryStartOffset=2F, _tryEndOffset=75, isSameTry=True, clauseKind=RH_EH_CLAUSE_TYPED
  EH clause to consider curIdx=4, _tryStartOffset=2F, _tryEndOffset=76, isSameTry=False, clauseKind=RH_EH_CLAUSE_FAULT

cc: @dotnet/jit-contrib

It goes back to the 8.0 behavior with set DOTNET_LegacyExceptionHandling=1 (which I learned about just now).