views:

797

answers:

2

Why is UnhandledExceptionEventArgs.ExceptionObject an object and not an Exception?

I am attaching to AppDomain.UnhandledException.

I would like to cast UnhandledExceptionEventArgs.ExceptionObject to an Exeption and interogate it.

And with this in mind will it ever be null?

The MSDN documentation is not exatly useful

http://msdn.microsoft.com/en-us/library/system.unhandledexceptioneventargs.exceptionobject.aspx

"Gets the unhandled exception object."

Thanks

+7  A: 

This cannot be typed to Exception because it's possible to throw objects in .Net that do not derive from System.Exception. This is not possible in C# or VB.Net but it is possible in other CLR based languages. Hence the API must support this possibility and uses the type object.

So while it shouldn't ever be null, it may not in fact be a System.Exception.

See CLI spec section 10.5 (specifically CLS rule 40) for more details

JaredPar
Thanks JaredI have added your answer and a link back here to the msdn community content
Simon
+4  A: 

In addition to what Jared has already mentioned, you can safely cast to Exception in versions 2.0 and higher of the .NET Framework if RuntimeCompatibilityAttribute(WrapNonExceptionThrows=true) has been applied to your assembly. (The attribute will be added automatically by the C# and VB compilers.) When the attribute has been applied, non-Exception "exceptions" will be wrapped in RuntimeWrappedException.

Nicole Calinoiu