O'Reilly logo

C# Cookbook by Jay Hilyard, Stephen Teilhet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.8. HRESULT-Exception Mapping

Problem

You need a reference table that maps each COM HRESULT to its managed exception counterpart. This mapping will allow you to throw the correct managed exception in C#, which will map to the expected COM HRESULT in unmanaged code.

Solution

Every managed exception maps to an HRESULT. Table 5-2 lists the managed exception classes and their equivalent HRESULT values. Use this table to determine what type of managed exception to use when throwing an exception back to unmanaged code, as well as what type of exception object to use when handling returned COM/COM+ HRESULT values.

Table 5-2. Mappping .NET exceptions to HRESULTS

.NET exception class name

HRESULT name (hex value of HRESULT)

AccessException

COR_E_MEMBERACCESS (0x8013151A)

AmbiguousMatchException

COR_E_AMBIGUOUSMATCH (0X80138000211D)

AppDomainUnloadedException

MSEE_E_APPDOMAINUNLOADED (0x80131015)

ApplicationException

COR_E_APPLICATION (0x80131600)

ArgumentException

COR_E_ARGUMENT (0x80070057 )

ArgumentNullException

E_POINTER (0x80004003)

ArgumentOutOfRangeException

COR_E_ARGUMENTOUTOFRANGE (0x80131502)

ArithmeticException

COR_E_ARITHMETIC (0x80070216)

ArrayTypeMismatchException

COR_E_ARRAYTYPEMISMATCH (0x80131503)

BadImageFormatException

COR_E_BADIMAGEFORMAT (0x8007000B)

CannotUnloadAppDomainException

COR_E_CANNOTUNLOADAPPDOMAIN (0x80131015)

COMException

Any Other HRESULT Defaults To This .NET Exception

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required