I discovered a scenario when my .Net application unexpectedly crashes without any exception. I would like at least an AppDomain.UnhandledException
work in this case, to allow me to at least register an error and present some information to the user.
In this scenario, I have a .Net assembly (let it be called A ) by calling interop on the native DLL (which we will call B ). B creates a flow and throws; If no one had caught the exception, I would have expected it to go all the way to the stack, back to my managed application, and finally become an unhandled exception.
At this point, I expected the OS to transfer control back to .Net, which would AppDomain.UnhandledException
, and with that my application would end. However, the call is never completed.
Below I have included enough information to reproduce the problem. It can be ignored.
Program.cs
internal class Program { public static void Main() { AppDomain.CurrentDomain.UnhandledException += _currentDomainUnhandledException; try { BugDllInterop.ErrorMethod(0); } catch (Exception e) { Console.WriteLine("Exception: " + e.GetType().Name); } Console.WriteLine("Clean exit."); } private static void _currentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine("Exception trapped."); } }
BugDllInterop.cs
public static class BugDllInterop { private const string BUG_DLL = "InteropExceptions.BugDll.dll"; [DllImport(BUG_DLL, CallingConvention = CallingConvention.Cdecl)] public extern static void ErrorMethod(int i); }
Bugdll.h
// INCLUSION GUARD
Bugdll.cpp
// HEADER /////////////////////////////////////////////////////////////////////
Again, I want the AppDomain.UnhandledException
event to be raised. Of course, if someone knows how to properly handle the exception in order to support the application, that would be even better.
source share