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)
But in the application I did compile; it just shows the application has crashed;
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...
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.
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).
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.
You can use the UnhandledException event of the AppDomain class to manage unhandled exceptions.
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With