System.AppDomain.UnhandledException Event

Occurs when an exception is not caught.

Syntax

public event UnhandledExceptionEventHandler UnhandledException

Remarks

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application. If sufficient information about the state of the application is available, other actions may be undertaken — such as saving program data for later recovery. Caution is advised, because program data can become corrupted when exceptions are not handled.

Note:

In the .NET Framework versions 1.0 and 1.1, application termination and debugging options are reported to the user before this event is raised, rather than after.

This event can be handled in any application domain. However, the event is not necessarily raised in the application domain where the exception occurred. An exception is unhandled only if the entire stack for the thread has been unwound without finding an applicable exception handler, so the first place the event can be raised is in the application domain where the thread originated.

Note:

In the .NET Framework versions 1.0 and 1.1, this event occurs only for the default application domain that is created by the system when an application is started. If an application creates additional application domains, specifying a delegate for this event in those applications domains has no effect.

If the AppDomain.UnhandledException event is handled in the default application domain, it is raised there for any unhandled exception in any thread, no matter what application domain the thread started in. If the thread started in an application domain that has an event handler for AppDomain.UnhandledException, the event is raised in that application domain. If that application domain is not the default application domain, and there is also an event handler in the default application domain, the event is raised in both application domains.

For example, suppose a thread starts in application domain "AD1", calls a method in application domain "AD2", and from there calls a method in application domain "AD3", where it throws an exception. The first application domain in which the AppDomain.UnhandledException event can be raised is "AD1". If that application domain is not the default application domain, the event can also be raised in the default application domain.

Note:

The common language runtime suspends thread aborts while event handlers for the AppDomain.UnhandledException event are executing.

If the event handler has a System.Runtime.ConstrainedExecution.ReliabilityContractAttribute attribute with the appropriate flags, the event handler is treated as a constrained execution region.

Starting with the net_v40_long, this event is not raised for exceptions that corrupt the state of the process, such as stack overflows or access violations, unless the event handler is security-critical and has the System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute attribute.

In the .NET Framework versions 1.0 and 1.1, an unhandled exception that occurs in a thread other than the main application thread is caught by the runtime and therefore does not cause the application to terminate. Thus, it is possible for the AppDomain.UnhandledException event to be raised without the application terminating. Starting with the .NET Framework version 2.0, this backstop for unhandled exceptions in child threads was removed, because the cumulative effect of such silent failures included performance degradation, corrupted data, and lockups, all of which were difficult to debug. For more information, including a list of cases in which the runtime does not terminate, see Exceptions in Managed Threads.

To register an event handler for this event, you must have the required permissions, or a System.Security.SecurityException is thrown.

For more information about handling events, see Events Overview.

Other Events for Unhandled Exceptions

For certain application models, the AppDomain.UnhandledException event can be preempted by other events if the unhandled exception occurs in the main application thread.

In applications that use Windows Forms, unhandled exceptions in the main application thread cause the System.Windows.Forms.Application.ThreadException event to be raised. If this event is handled, the default behavior is that the unhandled exception does not terminate the application, although the application is left in an unknown state. In that case, the AppDomain.UnhandledException event is not raised. This behavior can be changed by using the application configuration file, or by using the System.Windows.Forms.Application.SetUnhandledExceptionMode(System.Windows.Forms.UnhandledExceptionMode) method to change the mode to System.Windows.Forms.UnhandledExceptionMode.ThrowException before the System.Windows.Forms.Application.ThreadException event handler is hooked up. This applies only to the main application thread. The AppDomain.UnhandledException event is raised for unhandled exceptions thrown in other threads.

Starting with Microsoft Visual Studio 2005, the Visual Basic application framework provides another event for unhandled exceptions in the main application thread. See the Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.UnhandledException event. This event has an event arguments object with the same name as the event arguments object used by AppDomain.UnhandledException, but with different properties. In particular, this event arguments object has an Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs.ExitApplication property that allows the application to continue running, ignoring the unhandled exception (and leaving the application in an unknown state). In that case, the AppDomain.UnhandledException event is not raised.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0