.NET Framework Class Library
AppDomain.UnhandledException Event

Occurs when an exception is not caught.

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

Syntax

Visual Basic (Declaration)
Public Event UnhandledException As UnhandledExceptionEventHandler
Visual Basic (Usage)
Dim instance As AppDomain
Dim handler As UnhandledExceptionEventHandler

AddHandler instance.UnhandledException, handler
C#
public event UnhandledExceptionEventHandler UnhandledException
C++
public:
virtual event UnhandledExceptionEventHandler^ UnhandledException {
    void add (UnhandledExceptionEventHandler^ value) sealed;
    void remove (UnhandledExceptionEventHandler^ value) sealed;
}
J#
/** @event */
public final void add_UnhandledException (UnhandledExceptionEventHandler value)

/** @event */
public final void remove_UnhandledException (UnhandledExceptionEventHandler value)
JScript
JScript supports the use of events, but not the declaration of new ones.
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 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.

Example

The following sample demonstrates the UnhandledException event.

Visual Basic
Sub Main()
   Dim currentDomain As AppDomain = AppDomain.CurrentDomain
   AddHandler currentDomain.UnhandledException, AddressOf MyHandler
   
   Try
      Throw New Exception("1")
   Catch e As Exception
      Console.WriteLine("Catch clause caught : " + e.Message)
   End Try
   
   Throw New Exception("2")

   ' Output:
   '   Catch clause caught : 1
   '   MyHandler caught : 2
End Sub 'Main


Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
   Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
   Console.WriteLine("MyHandler caught : " + e.Message)
End Sub 'MyUnhandledExceptionEventHandler
C#
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();
   }
}
C++
public ref class Test
{
private:
   static void MyHandler( Object^ /*sender*/, UnhandledExceptionEventArgs^ args )
   {
      Exception^ e = dynamic_cast<Exception^>(args->ExceptionObject);
      Console::WriteLine( "MyHandler caught : {0}", e->Message );
   }
public: 
   [SecurityPermissionAttribute( SecurityAction::Demand, ControlAppDomain = true )]
   static void Main()
   {
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   currentDomain->UnhandledException += gcnew UnhandledExceptionEventHandler( Test::MyHandler );
   try
   {
      throw gcnew Exception( "1" );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Catch clause caught : {0}", e->Message );
   }

   throw gcnew Exception( "2" );
   
   // Output:
   //   Catch clause caught : 1
   //   MyHandler caught : 2
   }
};

int main()
{
   Test::Main();
}
.NET Framework Security

Platforms

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.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
See Also

Tags :


Community Content

Graham Knap
Call Environment.Exit() if you don't want to see the CLR's default exception handler
As the documentation states, "This event provides notification of uncaught exceptions." Handling this event does not actually handle uncaught exceptions -- so the system default handler will still be invoked after your event handler completes. One workaround is to call Environment.Exit() from your event handler.
Tags :

Page view tracker