I’ve developed a .NET 4 software and I’m ready to send it to beta users. If an unhandled exception is thrown in the software, I would like to catch it, log it and send the logs to me. I’ve already implemented this functionality and it seems to be running fine when I run it in debug mode with Visual Studio. However, when I’ve built a release version of the software and installed it, Microsoft .NET Framework starts to catch exceptions before my code. I get a popup with an error message: “Unhandled exception has occurred in a component in your application. If you click Continue, the application will ignore this error and attempt to continue.”
To test the crashing, I created a crash down button which throws an exception. This crash down logs itself and the exception handler logs all received unhandled exceptions. When I look at the log of the release version, I can only see the log message from crash down but not from the exception handler.
I’ve attached my own exception handler with this code:
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Is there some way to disable the exception catching of .NET Framework or is there a better way to attach my own exception handler?
UPDATE: I’m using WPF. I’ll look into the DispatcherUnhandledException and let you know if it solves the problem.
UPDATE #2: Unfortunately adding handler to Application.Current.DispatcherUnhandledException didn’t solve the problem. Apparently this debugging popup is created by JIT (Just-In-Time) debugger which is included with Visual Studio. I’ll have to test the software with a “civilian” Windows and see if the exceptions are catched there too.
UPDATE #3: For some reason the Release built with Visual Studio works but the Release built with MSBuild scripts and Dotfuscator does not.
I finally solved the problem. The problem was not caused by listening to wrong exceptions but due to missing a DLL from the released version.
After adding listeners for DispatchedUnhandledException and ThreadException events I no longer got the strange Microsoft .NET Framework popup which allowed the user to continue running the software after exception. However my own exception handling was still broken at this point.
Because the software was already crashing down at the moment when the exception handler was supposed to kick in, I had a catch (Exception) around the exception handler. After removing this catch I finally got the correct error message with release version and added the missing DLLs.
The lesson I learned (again) is: do not use empty catch (Exception) block. It is evil.