Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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

Parameters

mode
Type: System.Windows.Forms.UnhandledExceptionMode

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

ExceptionCondition
InvalidOperationException

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

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.