The application will not die with an unhandled exception?

If my application ends with an exception that I cannot handle, I want it to display an error message to the user and leave. The problem is that the user is shown a dialog with exceptions with the details, Continue, and Exit options, and if the user clicks Continue, the application remains in a β€œstate” that I do not want.

To replicate this with the least amount of code, I just created a Windows Forms application, added a button, and for the button click code I just wrote:

throw new ApplicationException("I must crash now!!!");

When I run the compiled exe from the Debug folder, the Release folder or run exe from the Release folder copied somewhere else (in case the pdb file causes a problem) and clicking on the button, I will be shown the details / Continue / Quit, and if I will say that the application remains alive. How can I prevent the continuation option from appearing?

I have this behavior on my computer (Vista, Visual Studio 2008, Visual Studio 2010, creating a test application with VS2010), as well as on a user computer (Windows 7).

+3
source share
2 answers

Windows Forms , . Windows Forms, , Application.SetUnhandledExceptionMode.

+4

AppDomain.UnhandledException, .

http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx

, , undefined, ... .

:

static void Main() {
    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.UnhandledException += errorHandler;
    // Then start your app!
}

static void errorHandler(object sender, UnhandledExceptionEventArgs args) {
    // Show the user some error, but be prepared for disaster...
    // Examine the args.ExceptionObject (which is an Exception)
}
+3

All Articles