UnhandledExceptionEventArgs.ExceptionObject Property     
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the unhandled exception object.
public:
 property System::Object ^ ExceptionObject { System::Object ^ get(); };public object ExceptionObject { get; }member this.ExceptionObject : objPublic ReadOnly Property ExceptionObject As ObjectProperty Value
The unhandled exception object.
Examples
The following example demonstrates the UnhandledException event. It defines an event handler, MyHandler, that is invoked whenever an unhandled exception is thrown in the default application domain. It then throws two exceptions. The first is handled by a try/catch block. The second is unhandled and invokes the MyHandle routine before the application terminates.
using System;
public class Example
{
   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 : {0} \n", e.Message);
      }
      throw new Exception("2");
   }
   static void MyHandler(object sender, UnhandledExceptionEventArgs args)
   {
      Exception e = (Exception) args.ExceptionObject;
      Console.WriteLine("MyHandler caught : " + e.Message);
      Console.WriteLine("Runtime terminating: {0}", args.IsTerminating);
   }
}
// The example displays the following output:
//       Catch clause caught : 1
//
//       MyHandler caught : 2
//       Runtime terminating: True
//
//       Unhandled Exception: System.Exception: 2
//          at Example.Main()
open System
open System.Security.Permissions
let myHandler _ (args: UnhandledExceptionEventArgs) =
    let e = args.ExceptionObject :?> Exception
    printfn $"MyHandler caught : {e.Message}"
    printfn $"Runtime terminating: {args.IsTerminating}"
[<EntryPoint>]
let main _ =
    let currentDomain = AppDomain.CurrentDomain
    currentDomain.UnhandledException.AddHandler(UnhandledExceptionEventHandler myHandler)
    try
        failwith "1"
    with e ->
        printfn $"Catch clause caught : {e.Message} \n"
    failwith "2"
// The example displays the following output:
//       Catch clause caught : 1
//
//       MyHandler caught : 2
//       Runtime terminating: True
//
//       Unhandled Exception: System.Exception: 2
//          at Example.main()
Module Example
   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)
         Console.WriteLine()
      End Try
      
      Throw New Exception("2")
   End Sub
   
   Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
      Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
      Console.WriteLine("MyHandler caught : " + e.Message)
      Console.WriteLine("Runtime terminating: {0}", args.IsTerminating)
   End Sub
End Module
' The example displays the following output:
'       Catch clause caught : 1
'       
'       MyHandler caught : 2
'       Runtime terminating: True
'       
'       Unhandled Exception: System.Exception: 2
'          at Example.Main()
Remarks
This property returns an object of type Object rather than one derived from Exception. Although the Common Language Specification requires that all exception types derive from Exception, it is possible for methods to throw exceptions with objects not derived from Exception. You can do the following to work with this exception:
- Apply the RuntimeCompatibilityAttribute attribute with a RuntimeCompatibilityAttribute.WrapNonExceptionThrows value of - trueto the assembly that contains the event handler. This wraps all exceptions not derived from the Exception class in a RuntimeWrappedException object. You can then safely cast (in C#) or convert (in Visual Basic) the object returned by this property to an Exception object, and retrieve the original exception object from the RuntimeWrappedException.WrappedException property. Note that some compilers, such as the C# and Visual Basic compilers, automatically apply this attribute.
- Cast the object returned by this property to an Exception object.