views:

36

answers:

2

When using Marshal.GetExceptionCode() how do you get the Exception Type and/or instance of the exception?

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getexceptioncode%28VS.71%29.aspx

A: 

I believe you want GetExceptionForHR, but it is only available for .NET 2.0 and above.

Mark Hurd
A: 

That's not how it works. Marshal.GetExceptionCode only returns a meaningful number if the CLR has caught an SEH exception. It will try to translate the exception into a meaningful managed exception. The common ones are NullReferenceException, OverflowException, DivideByZeroException, StackOverflowException, AccessViolationException. SEHException is the fall-back one.

You can call GetExceptionCode while handling one of those exceptions. Chicken-and-egg, the managed exception gets raised first.

Hans Passant