UnhandledException Event
Collapse the table of content
Expand the table of content

AppDomain.UnhandledException Event

Occurs when an exception is not caught by an event handler.

[Visual Basic]
Public Overridable Event UnhandledException As _
   UnhandledExceptionEventHandler
[C#]
public virtual event UnhandledExceptionEventHandler
   UnhandledException;
[C++]
public: virtual __event UnhandledExceptionEventHandler*
   UnhandledException;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type UnhandledExceptionEventArgs containing data related to this event. The following UnhandledExceptionEventArgs properties provide information specific to this event.

Property Description
ExceptionObject Gets the unhandled exception object.
IsTerminating Indicates whether the common language runtime is terminating.

Remarks

The UnhandledExceptionEventHandler delegate for this event provides default handling for uncaught exceptions. When this event is not handled, the system default handler reports the exception to the user and terminates the application.

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.

To register an event handler for this event, you must have the permissions described in the Permissions section. If you do not have the appropriate permissions, a SecurityException occurs.

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, C++] 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#] 
public static void Main() {
   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);
}

[C++] 
public __gc class Test {
public:   
   static void MyHandler(Object* /*sender*/, UnhandledExceptionEventArgs* args) {
      Exception* e = dynamic_cast<Exception*> (args->ExceptionObject);
      Console::WriteLine(S"MyHandler caught : {0}", e->Message);
   }
};

int main() {
   AppDomain* currentDomain = AppDomain::CurrentDomain;
   currentDomain->UnhandledException += new UnhandledExceptionEventHandler(0, Test::MyHandler);

   try {
      throw new Exception(S"1");
   } catch (Exception* e) {
      Console::WriteLine(S"Catch clause caught : {0}", e->Message);
   }

   throw new Exception(S"2");

   // Output:
   //   Catch clause caught : 1
   //   MyHandler caught : 2
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

AppDomain Class | AppDomain Members | System Namespace

Show:
© 2016 Microsoft