7

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.

Sumeshk
  • 1,980
  • 20
  • 33
  • Well and is this piece of code called before exception happens in your .exe file ? – Ondrej Svejdar Jan 06 '14 at 10:05
  • In mY dll there is class (say "abc") ,while creating the object of abc appDomain.UnhandledException is initialized ,and in my main() i created the object of abc. – Sumeshk Jan 06 '14 at 10:11

1 Answers1

6

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);
ken2k
  • 48,145
  • 10
  • 116
  • 176
Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • How would you achieve the same end result in vb.net? I found in [the answer by nobugz on this thread](https://social.msdn.microsoft.com/Forums/windows/en-US/9dbbe3b6-2e92-4a98-be62-28c933a7741e/error-calling-applicationsetunhandledexceptionmode?forum=winforms) that we have to disable the application framework and add a Module with Sub Main to run `SetUnhandledExceptionMode()` without an Exception. But I already have a defined tryblock that calls a function from my referenced DLL, and I'd like that immediate tryblock to handle the Exception thrown by the DLL, not a new event handler function. – Chad Nov 14 '19 at 00:00