Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What controls the default UnhandledException policy in WinForms?

I am currently maintaining a WinForms application that listens to the Application.ThreadException event to trap unhandled exceptions on GUI threads.

Now things have been working as expected until recently. But lately, this event is no longer being raised appropriately on some production boxes; the app skips the handler and just crashes when there is an unhandled exception on a GUI thread. Strangely, I am able to reproduce this on my (new) dev box, but there are some machines on which the event is in fact being raised correctly.

I am able to make the behaviour consistent by explicitly setting the policy like this:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

But I am curious to know what controls the default policy. MSDN makes vague allusions to an "application configuration file", but there is no such policy-setting in our app.config or any other configuration file that I know of.

What is causing this inconsistent behaviour?

like image 635
Ani Avatar asked Sep 04 '11 09:09

Ani


People also ask

What causes Unhandled exception errors?

An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist. The . NET Framework will then throw a FileNotFoundException.

What is user Unhandled exception?

An unhandled exception is an error in a computer program or application when the code has no appropriate handling exceptions.

How to get Unhandled exception in c#?

Find Unhandled Exceptions With Retrace One of the great features of Retrace is its error monitoring capabilities. Retrace can automatically collect all . NET exceptions that are occurring within your application. This includes unhandled exceptions but can also include all thrown exceptions, or first chance exceptions.

What is an Unhandled exception in java?

Java provides complete support to handle the exceptions so that code can run smoothly without termination and provide the desired result. An exception that is not handled is called an unhandled exception and leads to terminating the code before its execution.


1 Answers

There's only one that I can think of. The exception handling logic is aware whether or not a debugger is attached. That matters when the default UnhandledExceptionMode.Automatic is in effect. When a debugger is attached, the Winforms message loop does not try to catch exceptions. Which is fairly important, it would make debugging exceptions rather difficult. The debugger only steps in and displays the Exception Assistant when an exception is unhandled.

Using UnhandledExceptionMode.CatchException is okay, it makes exception handling consistent. In other words, it will behave on your dev machine exactly the same as it does on your client's machine. But you'll now need Debug + Exceptions, Thrown box to troubleshoot the code. That always makes the debugger stop when the exception is thrown, regardless of whether it is caught or not.

like image 179
Hans Passant Avatar answered Oct 03 '22 16:10

Hans Passant