I had a unit test (MSTest) that threw an exception and triggered the AppDomain.CurrentDomain.UnhandledException event.
This worked in .Net Framework 4.8 (the method DoSmth
was called):
AppDomain.CurrentDomain.UnhandledException += (_, args) => DoSmth();
var thread = new Thread(() => throw new DataException("This is just a system test")); // necessary. Otherwise, the unit test framework will see the exception and the test fails.
thread.Start();
thread.Join();
But it does not work in .Net5 or 6.
The whole test execution crashes with:
The active test run was aborted. Reason: Test host process crashed : Unhandled exception. System.Data.DataException: This is just a system test
Does someone know how to fix it for .Net5/6?