What controls the default UnhandledException policy in WinForms?

I currently support a WinForms application that listens for the Application.ThreadException event to catch unhandled exceptions in GUI threads.

Now everything works as expected until recently. But recently, this event no longer rises properly on some food crates; the application skips the handler and just crashes when there is an unhandled exception in the GUI thread. Oddly enough, I can reproduce this in my (new) dev box, but there are some machines on which the event is actually correctly created.

I can make the behavior consistent by explicitly setting the policy as follows:

 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 

But I'm curious to know what controls the default policy. MSDN makes vague allusions to the "application configuration file", but such a policy does not exist in our app.config or any other configuration file that I know of.

What causes this inconsistent behavior?

+6
source share
2 answers

There is only one that I can think of. Exception logic knows if a debugger is connected. This matters when the default is UnhandledExceptionMode.Automatic. When the debugger is connected, the Winforms message loop does not attempt to catch exceptions. Which is pretty important, this will make debugging exceptions quite complicated. The debugger performs only the steps and displays the Exception Assistant if the exception is not handled.

Using UnhandledExceptionMode.CatchException is ok, this makes exception handling consistent. In other words, it will behave on your dev machine exactly the same as on your client machine. But now you will need Debug + Exceptions, Thrown box to troubleshoot code issues. This always causes the debugger to stop when an exception is thrown, whether it is caught or not.

+1
source

I also began to experience this. We tried to debug exceptions that our testers saw and could not reproduce on our own machines. I enabled stop-on-exceptions behavior in VS and alt, exceptions actually occurred. However, if I were in F11 to continue, I would see that exceptions are simply ignored.

I have two cars. One machine is Win 7 64 bit, VS 2008 and VS 2010 are installed, and therefore .Net 4.0 is installed. The application we are testing is .Net 3.5 and is debugged in VS 2008.

Another computer is 32-bit Win XP, simple jane VS 2008 and .Net 3.5.

The Win 7 machine silently ignores. The XP device complains loudly. I think that when installing .Net 4.0, the default policy was changed.

I worked on it by calling

 Application.SetUnhandledExceptionMode( UnhandledExceptionMode.CatchException ); 
0
source

Source: https://habr.com/ru/post/896577/


All Articles