Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

AppDomain.UnhandledException Event

Occurs when an exception is not caught.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public event UnhandledExceptionEventHandler UnhandledException
/** @event */
public final void add_UnhandledException (UnhandledExceptionEventHandler value)

/** @event */
public final void remove_UnhandledException (UnhandledExceptionEventHandler value)

JScript supports the use of events, but not the declaration of new ones.

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 an unhandled exception can corrupt program data irretrievably.

NoteNote

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.

This event can be handled in any application domain. If it is handled in the default application domain, it is raised for any unhandled exception in any application domain. If it is handled in a child domain, it is raised only for unhandled exceptions in that domain. If the event is handled in both the default domain and in a child domain, an unhandled exception in the child domain causes the event to be raised in both domains.

NoteNote

In the .NET Framework versions 1.0 and 1.1, this event occurs only for the 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.

In the .NET Framework versions 1.0 and 1.1, an unhandled exception that occurred in a thread other than the main application thread was caught by the runtime and therefore did not cause the application to terminate. Thus, it was possible for the UnhandledException event to be raised without the application terminating. In 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, see Exceptions in Managed Threads.

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

For more information about handling events, see Consuming Events.

The following sample demonstrates the UnhandledException event.

using System;
using System.Security.Permissions;

public class Test {

   [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
   public static void Example()
   {
      AppDomain currentDomain = AppDomain.CurrentDomain;
      currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
      
      try {
         throw new Exception("1");
      } catch (Exception e) {
         Console.WriteLine("Catch clause caught : " + e.Message);
      }

      throw new Exception("2");

      // Output:
      //   Catch clause caught : 1
      //   MyHandler caught : 2
   }
   
   static void MyHandler(object sender, UnhandledExceptionEventArgs args) {
      Exception e = (Exception) args.ExceptionObject;
      Console.WriteLine("MyHandler caught : " + e.Message);
   }

   public static void Main() {
      Example();
   }
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.