Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows

When I create a new project, I get a strange behavior for unhandled exceptions. This is how I can reproduce the problem:

1) create a new Windows Forms Application (C#, .NET Framework 4, VS2010)

2) add the following code to the Form1_Load handler:

int vara = 5, varb = 0; int varc = vara / varb; int vard = 7; 

I would expect that VS breaks and shows an unhandled exception message at the second line. However, what happens is that the third line is just skipped without any message and the application keeps running.

I don't have this problem with my existing C# projects. So I guess that my new projects are created with some strange default settings.

Does anyone have an idea what's wrong with my project???

I tried checking the boxes in Debug->Exceptions. But then executions breaks even if I handle the exception in a try-catch block; which is also not what I want. If I remember correctly, there was a column called "unhandled exceptions" or something like this in this dialog box, which would do excatly what I want. But in my projects there is only one column ("Thrown").

like image 517
Robert Hegner Avatar asked Feb 08 '11 14:02

Robert Hegner


People also ask

Where do I find unhandled exceptions?

If your application has unhandled exceptions, that may be logged in the Windows Event Viewer under the category of “Application”. This can be helpful if you can't figure out why your application suddenly crashes. Windows Event Viewer may log 2 different entries for the same exception.

Does Microsoft still support WinForms?

"We continue to support and innovate in Windows Forms runtime," said Microsoft's Igor Velikorossov last month in announcing what's new for WinForms in . NET 6. He's a software engineer on the dev team for the 19-year-old product, a free and open-source graphical (GUI) class library included as a part of .


2 Answers

This is a nasty problem induced by the wow64 emulation layer that allows 32-bit code to run on the 64-bit version of Windows 7. It swallows exceptions in the code that runs in response to a notification generated by the 64-bit window manager, like the Load event. Preventing the debugger from seeing it and stepping in. This problem is hard to fix, the Windows and DevDiv groups at Microsoft are pointing fingers back and forth. DevDiv can't do anything about it, Windows thinks it is the correct and documented behavior, mysterious as that sounds.

It is certainly documented but just about nobody understands the consequences or thinks it is reasonable behavior. Especially not when the window procedure is hidden from view of course, like it is in any project that uses wrapper classes to hide the window plumbing. Like any Winforms, WPF or MFC app. Underlying issue is Microsoft could not figure out how to flow exceptions from 32-bit code back to the 64-bit code that triggered the notification back to 32-bit code that tries to handle or debug the exception.

It is only a problem with a debugger attached, your code will bomb as usual without one.

Project > Properties > Build tab > Platform target = AnyCPU and untick Prefer 32-bit. Your app will now run as a 64-bit process, eliminating the wow64 failure mode. Some consequences, it disables Edit + Continue for VS versions prior to VS2013 and might not always be possible when you have a dependency on 32-bit code.

Other possible workarounds:

  • Debug > Exceptions > tick the Thrown box for CLR exceptions to force the debugger to stop at the line of code that throws the exception.
  • Write try/catch in the Load event handler and failfast in the catch block.
  • Use Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) in the Main() method so that the exception trap in the message loop isn't disabled in debug mode. This however makes all unhandled exceptions hard to debug, the ThreadException event is pretty useless.
  • Consider if your code really belongs in the Load event handler. It is very rare to need it, it is however very popular in VB.NET and a swan song because it is the default event and a double-click trivially adds the event handler. You only ever really need Load when you are interested in the actual window size after user preferences and autoscaling is applied. Everything else belongs in the constructor.
  • Update to Windows 8 or later, they have this wow64 problem solved.
like image 110
Hans Passant Avatar answered Oct 06 '22 13:10

Hans Passant


In my experience, I only see this issue when I'm running with a debugger attached. The application behaves the same when run standalone: the exception is not swallowed.

With the introduction of KB976038, you can make this work as you'd expect again. I never installed the hotfix, so I'm assuming it came as part of Win7 SP1.

This was mentioned in this post:

  • The case of the disappearing OnLoad exception – user-mode callback exceptions in x64

Here's some code that will enable the hotfix:

public static class Kernel32 {     public const uint PROCESS_CALLBACK_FILTER_ENABLED = 0x1;      [DllImport("Kernel32.dll")]     public static extern bool SetProcessUserModeExceptionPolicy(UInt32 dwFlags);      [DllImport("Kernel32.dll")]     public static extern bool GetProcessUserModeExceptionPolicy(out UInt32 lpFlags);       public static void DisableUMCallbackFilter() {         uint flags;         GetProcessUserModeExceptionPolicy(out flags);          flags &= ~PROCESS_CALLBACK_FILTER_ENABLED;         SetProcessUserModeExceptionPolicy(flags);     } } 

Call it at the beginning of your application:

    [STAThread]     static void Main()     {         Kernel32.DisableUMCallbackFilter();          Application.EnableVisualStyles();         Application.SetCompatibleTextRenderingDefault(false);         Application.Run(new Form1());     } 

I've confirmed (with the the simple example shown below) that this works, just as you'd expect.

protected override void OnLoad(EventArgs e) {     throw new Exception("BOOM");   // This will now get caught. } 

So, what I don't understand, is why it was previously impossible for the debugger to handle crossing kernel-mode stack frames, but with this hotfix, they somehow figured it out.

like image 39
Jonathon Reinhart Avatar answered Oct 06 '22 13:10

Jonathon Reinhart