How to disable .NET Framework exception handling and use my own instead? How to disable .NET Framework exception handling and use my own instead? windows windows

How to disable .NET Framework exception handling and use my own instead?


You haven't specified which framework you're using, but there are other "unhandled exception" events around.

For Windows Forms, there's Application.ThreadException.

For WPF/Silverlight there's Application.DispatcherUnhandledException.

Try one of those two first, and let us know if you're still having problems.


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.


It sounds like the exception is bubbling out to your application's message loop. In Windows Forms, you can handle these by setting up an event handler for Application.ThreadException event. In WPF/Silverlight, the equivalent event would be Application.DispatcherUnhandledException.

You can also put a try/catch in your Main method (if you have one) for good measure but the UI will usually catch the exceptions first as you've noticed.

EDIT

In WPF/Silverlight, set e.Handled = true to prevent the exception from continuing up the stack.