UnhandledExceptionEventArgs::ExceptionObject Property
Gets the unhandled exception object.
Assembly: mscorlib (in mscorlib.dll)
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 true to 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.
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.
// The example should be compiled with the /clr:pure compiler option. using namespace System; using namespace System::Security::Permissions; public ref class Example { private: static void MyHandler(Object^ sender, UnhandledExceptionEventArgs^ args) { Exception^ e = dynamic_cast<Exception^>(args->ExceptionObject); Console::WriteLine( "MyHandler caught : {0}", e->Message ); Console::WriteLine("Runtime terminating: {0}", args->IsTerminating); } public: [SecurityPermissionAttribute( SecurityAction::Demand, ControlAppDomain = true )] static void Main() { AppDomain^ currentDomain = AppDomain::CurrentDomain; currentDomain->UnhandledException += gcnew UnhandledExceptionEventHandler(Example::MyHandler); try { throw gcnew Exception("1"); } catch (Exception^ e) { Console::WriteLine( "Catch clause caught : {0}\n", e->Message ); } throw gcnew Exception("2"); } }; void main() { Example::Main(); } // The example displays the following output: // Catch clause caught : 1 // // MyHandler caught : 2 // Runtime terminating: True // // Unhandled Exception: System.Exception: 2 // at Example.Main() // at mainCRTStartup(String[] arguments)
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0