Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a setting that is preventing the unhandled exception dialog from displaying in apps that I compile?

I'm not sure if this is the right place to ask but I shall anyway.

I have 2 .NET applications; one that was compiled by myself, the other not. Both use .NET Framework 4.5. But they handle exceptions differently.

In the application which I did NOT compile; it shows the unhandled exception dialog (which I want and expect)

Unhandled Exception from app I didnt compile

But in the application I did compile; it just shows the application has crashed;

Unhandled Exepction from app I compiled

So there must be a setting in the VS config or project config that is preventing the unhandled exception dialog from displaying in apps that I compile?...

I have tried re-installing VS, changing the settings in the Debug->Exceptions menu, neither have worked...

like image 566
craig1231 Avatar asked Jul 21 '12 09:07

craig1231


People also ask

How do you handle unhandled exception?

The . NET Framework provides a couple events that can be used to catch unhandled exceptions. You only need to register for these events once in your code when your application starts up. For ASP.NET, you would do this in the Startup class or Global.

How do I fix unhandled exception in Visual Studio?

To change this setting for a particular exception, select the exception, right-click to show the shortcut menu, and select Continue When Unhandled in User Code. You may also change the setting for an entire category of exceptions, such as the entire Common Language Runtime exceptions).

Which page is called when unhandled exception occurs?

When an unhandled exception occurs in an ASP.NET application, it bubbles up to the ASP.NET runtime, which raises the Error event and displays the appropriate error page. There are three different types of error pages: the Runtime Error Yellow Screen of Death (YSOD); the Exception Details YSOD; and custom error pages.

Which event is used for unhandled exceptions?

You can use the UnhandledException event of the AppDomain class to manage unhandled exceptions.


2 Answers

The top screen-shot is a ThreadExceptionDialog. It is displayed in the very specific case where a Winforms app bombs in an event handler that was fired by the message loop (Application.Run) and the app didn't otherwise reassign the Application.ThreadException event handler. Using it is not a very good practice, there's no reasonable way the user could know whether to click the Continue or Quit button. Be sure to call Application.SetUnhandledExceptionMode() to disable it.

The bottom screen-shot is the default Windows error reporting dialog, displayed by Windows when a program bombed on an unhandled exception. You should never let it get to this point, the dialog doesn't display enough information to help anybody diagnose and fix the problem. Always write an event handler for the AppDomain.CurrentDomain.UnhandledException event. Display and/or log e.ExceptionObject.ToString() and call Environment.Exit() to terminate the app.

Make your Program.cs source code look similar to this:

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        Application.Run(new Form1());
    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
        // TODO: improve logging and reporting
        MessageBox.Show(e.ExceptionObject.ToString());
        Environment.Exit(-1);
    }
like image 100
Hans Passant Avatar answered Sep 22 '22 20:09

Hans Passant


The solution presented by Hans Passant will terminate the application. This also means that remaining finally blocks are not executed any more.

You can also use PInvoke to change the behavior by calling the SetErrorMode() method.

public enum ErrorModes : uint
{
    SYSTEM_DEFAULT = 0x0,
    SEM_FAILCRITICALERRORS = 0x0001,
    SEM_NOALIGNMENTFAULTEXCEPT = 0x0004,
    SEM_NOGPFAULTERRORBOX = 0x0002,
    SEM_NOOPENFILEERRORBOX = 0x8000,
    SEM_NONE = SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX
}

[DllImport("kernel32.dll")]
static extern ErrorModes SetErrorMode(ErrorModes uMode);

and then call

SetErrorMode(ErrorModes.SEM_NONE);

Doing so will give the finally blocks the chance to run.

like image 29
Thomas Weller Avatar answered Sep 24 '22 20:09

Thomas Weller