Application.SetUnhandledExceptionMode Method (UnhandledExceptionMode)


Instructs the application how to respond to unhandled exceptions.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

static member SetUnhandledExceptionMode : 
        mode:UnhandledExceptionMode -> unit


Type: System.Windows.Forms.UnhandledExceptionMode

An UnhandledExceptionMode value describing how the application should behave if an exception is thrown without being caught.

Exception Condition

You cannot set the exception mode after the application has created its first window.

It is often not feasible to catch all of the exceptions thrown by Windows Forms. Using this method, you can instruct your application whether it should catch all unhandled exceptions thrown by Windows Forms components and continue operating, or whether it should expose them to the user and halt execution.

Call SetUnhandledExceptionMode before you instantiate the main form of your application using the Run method.

To catch exceptions that occur in threads not created and owned by Windows Forms, use the UnhandledException event handler.

The following code example sets event handlers for exceptions that occur on Windows Forms threads and exceptions that occur on other threads. It sets SetUnhandledExceptionMode so that all exceptions are handled by the application, regardless of the settings in the application's user configuration file. It uses the ThreadException event to handle UI thread exceptions, and the UnhandledException event to handle non-UI thread exceptions. Since UnhandledException cannot prevent an application from terminating, the example simply logs the error in the application event log before termination.

This example assumes that you have defined two Button controls, button1 and button2, on your Form class.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Return to top