Search code examples
c#.netexceptiondlluncaughtexceptionhandler

Handle unhandled exception from a reference dll


I have created a c# dll to handle all the unhandled exceptions from the application.

added

AppDomain appDomain = AppDomain.CurrentDomain;
appDomain.UnhandledException += new UnhandledExceptionEventHandler(MyErrorHandler);

code in my dll project , added reference to my application.

while debugging if my application throws an unhanlded exception it is automatically caught from the dll and i successfully logged to a file.

But when my application is deployed ( or execute my application directly ( double click the exe)) the dll is not able to catch the unhandled exception from the application.


Solution

  • See this on MSDN

    You can try to use the add handler to threadException of the application and also the CurrentDomain Unhandled Exception like you write in your code

    Application.ThreadException += new ThreadExceptionEventHandler(Error_.MyExc);
    Application.SetUnhandledExceptionMode(Error_.MyCatchExc);
    
    // from your code 
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyErrorHandler);