Skip to content

Faster Exceptions: unmanaged exception handling in C++/CLI #114727

Closed

Description

@panxn

Description

With .NET9 Faster Exceptions, the exception handling for unmanaged types is broken in C++/CLI.

The issue can be described with these code:

    // C++/CLI
    try
    {
        throw std::exception();
    }
    catch (const std::exception& e)
    {
        // net8 runs here, or use LegacyExceptionHandling
    }
    catch (System::Exception^ e)
    {
        // net9/10 runs here
    }

It's not intuitive and doesn't match the statement about catching unmanaged exception in c++/cli.

Using System.Runtime.LegacyExceptionHandling can fix the problem, but the configuration will be removed in future. So I hope the problem can be fixed in "Faster Exceptions".

Reproduction Steps

See above

Expected behavior

C++/CLI should be able to handle unmanaged exception.

Actual behavior

Faster Exceptions transfers unmanaged exception to managed exception.

Regression?

No response

Known Workarounds

set System.Runtime.LegacyExceptionHandling to true

Configuration

No response

Other information

No response

Activity

  1. ghost added
    area-ExceptionHandling-coreclr
    on Apr 16, 2025
  2. added
    untriaged
    on Apr 16, 2025
  3. AaronRobinsonMSFT commented on Apr 20, 2025

    @AaronRobinsonMSFT
    Member

    @janvorli What is the way forward here? I didn't think we would be regressing C++/CLI by default.

  4. AaronRobinsonMSFT commented on Apr 20, 2025

    @AaronRobinsonMSFT
    Member

    @panxn Can you also please share how you are compiling your C++/CLI? Specifically what /clr flag setting are you using? I'd like to better understand community usage in general.

  5. filipnavara commented on Apr 20, 2025

    @filipnavara
    Member

    I managed to reproduce this in a small project (/clr:netcore library invoked from C# executable). There's nothing fundamentally incompatible (as in, should be easy to fix), the C++/CLI code throws the exception from native code, it enters the managed code which wraps it, and then it uses a filter to identify the C++ exceptions (based on Marshal.GetExceptionPointers()).

  6. filipnavara commented on Apr 20, 2025

    @filipnavara
    Member

    Here are binaries of the repro, the build process is a bit convoluted: cppcli-exception.zip

    It seems to pass on latest .NET 10 (main, not tested on Preview 3) but extra verification would be welcome.

  7. panxn commented on Apr 21, 2025

    @panxn
    Author

    I made a code sample to reproduce it CppCliException.zip:

    NetConsoleApp.csproj is .NET Console App, it was created from Visual Studio 2022 17.12
    Net8CppCliDll.vcxproj is C++/CLI DLL, also created from VS, project type is "CLR Empty Project (.NET)"

    The dotnet console app calls function defined in c++/cli. Exceptions are thrown and caught in c++/cli.

    Modify NetConsoleApp.csproj to use net8 or net9/10, or enable System.Runtime.LegacyExceptionHandling with net9/10 to watch the difference.

    This is a typical usage:

    std::map<int, int> m;
    try {
        m.at(1);
    } catch (std::out_of_range) {
        // should be caught in c++/cli
    }
    
  8. self-assigned this
    on Apr 23, 2025
  9. janvorli commented on Apr 23, 2025

    @janvorli
    Member

    Let me take a look. I have recently fixed a bug in the native exception handling (#113323), chances are this issue is related.

  10. janvorli commented on Apr 23, 2025

    @janvorli
    Member

    I can confirm that the issue was fix by my recent change. The test app that @panxn shared above prints "std::exception" with the current main branch while it prints "System::Exception" with older .NET 10. The change missed preview 3 by a couple of days, so it will be in preview 4.

  11. AaronRobinsonMSFT commented on Apr 23, 2025

    @AaronRobinsonMSFT
    Member

    Thanks @janvorli. Looks like we can close this. @panxn please consider using .NET 10 Preview 4 to validate these changes. Let's track any servicing of .NET 9 in the PR that fixes the issue.

  12. removed
    untriaged
    on Apr 23, 2025
  13. panxn commented on Apr 24, 2025

    @panxn
    Author

    Thank you! I will use preview 4.

  14. locked and limited conversation to collaborators on May 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-ExceptionHandling-coreclr

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions