DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 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

ContextMarshalException

COR_E_CONTEXTMARSHAL (0x80131504)

CryptographicException

NTE_FAIL

CryptographicUnexpectedOperationException

CORSEC_E_CRYPTO_UNEX_OPER (0x80131431)

CustomAttributeFormatException

COR_E_FORMAT (0x80131537)

DirectoryNotFoundException

COR_E_DIRECTORYNOTFOUND (0x80070003)

STG_E_PATHNOTFOUND (0x80030003)

DivideByZeroException

COR_E_DIVIDEBYZERO (0x80020012)

DllNotFoundException

COR_E_DLLNOTFOUND (0x80131524)

DuplicateWaitObjectException

COR_E_DUPLICATEWAITOBJECT (0x80131529)

EndOfStreamException

COR_E_ENDOFSTREAM (0x801338)

EntryPointNotFoundException

COR_E_TYPELOAD (0x80131522)

Exception

COR_E_EXCEPTION (0x80131500)

ExecutionEngineException

COR_E_EXECUTIONENGINE (0x80131506)

ExternalException

E_FAIL (0x80004005)

FieldAccessException

COR_E_FIELDACCESS (0x80131507)

FileNotFoundException

COR_E_FILELOAD (0x80131621 or 0x80131018)

FileNotFoundException

COR_E_FILENOTFOUND (0x80070002)

FormatException

COR_E_FORMAT (0x80131537)

IndexOutOfRangeException

COR_E_INDEXOUTOFRANGE (0x80131508)

InvalidCastException

COR_E_INVALIDCAST (0x80004002)

InvalidComObjectException

COR_E_INVALIDCOMOBJECT (0x80131527)

InvalidFilterCriteriaException

COR_E_INVALIDFILTERCRITERIA (0x80131601)

InvalidOleVariantTypeException

COR_E_INVALIDOLEVARIANTTYPE (0x80131531)

InvalidOperationException

COR_E_INVALIDOPERATION (0x80131509)

InvalidProgramException

COR_E_INVALIDPROGRAM (0x8013153A)

IOException

COR_E_IO (0x80131620)

IsolatedStorageException

ISS_E_ISOSTORE (0x80131450)

MarshalDirectiveException

COR_E_MARSHALDIRECTIVE (0x80131535)

MethodAccessException

COR_E_METHODACCESS (0x80131510)

MissingFieldException

COR_E_MISSINGFIELD (0x80131511)

MissingManifestResourceException

COR_E_MISSINGMANIFESTRESOURCE (0x80131532)

MissingMemberException

COR_E_MISSINGMEMBER (0x80131512)

MissingMethodException

COR_E_MISSINGMETHOD (0x80131513)

MulticastNotSupportedException

COR_E_MULTICASTNOTSUPPORTED (0x80131514)

NotFiniteNumberException

COR_E_NOTFINITENUMBER (0x80131528)

NotImplementedException

E_NOTIMPL

NotSupportedException

COR_E_NOTSUPPORTED (0x80131515)

NullReferenceException

COR_E_NULLREFERENCE (0x80004003)

OutOfMemoryException

COR_E_OUTOFMEMORY (0x8007000E)

OverflowException

COR_E_OVERFLOW (0x80131516)

PathTooLongException

COR_E_PATHTOOLONG (0x8013206)

PlatformNotSupportedException

COR_E_PLATFORMNOTSUPPORTED (0x80131539)

PolicyException

CORSEC_E_POLICY_EXCEPTION

RankException

COR_E_RANK (0x80131517)

ReflectionTypeLoadException

COR_E_REFLECTIONTYPELOAD (0x80131602)

RemotingException

COR_E_REMOTING (0x8013150B)

RemotingTimeoutException

COR_E_REMOTING (0x8013150B)

SafeArrayTypeMismatchException

COR_E_SAFEARRAYTYPEMISMATCH (0x80131533)

SafeArrayRankMismatchException

COR_E_SAFEARRAYRANKMISMATCH (0x80131538)

SecurityException

COR_E_SECURITY (0x8013150A)

SEHException

E_FAIL (0x80004005)

SerializationException

COR_E_SERIALIZATION (0x8013150C)

ServerException

COR_E_SERVER (0x8013150E)

StackOverflowException

COR_E_STACKOVERFLOW (0x800703E9)

SUDSGeneratorException

COR_E_EXCEPTION (0x80131500)

SUDSParserException

COR_E_EXCEPTION (0x80131500)

SynchronizationLockException

COR_E_SYNCHRONIZATIONLOCK (0x80131518)

SystemException

COR_E_SYSTEM (0x80131501)

TargetException

COR_E_TARGET

TargetInvocationException

COR_E_TARGETINVOCATION (0x80131604)

TargetParameterCountException

COR_E_TARGETPARAMCOUNT (0x80138002)

ThreadAbortException

COR_E_THREADABORTED (0x80131530)

ThreadInterruptedException

COR_E_THREADINTERRUPTED (0x80131519)

ThreadStateException

COR_E_THREADSTATE (0x80131520)

ThreadStopException

COR_E_THREADSTOP

TypeInitializationException

COR_E_TYPEINITIALIZATION (0x80131534)

TypeLoadException

COR_E_TYPELOAD (0x80131522)

TypeUnloadedException

COR_E_TYPEUNLOADED (0x80131013)

UnauthorizedAccessException

COR_E_UNAUTHORIZEDACCESS (0x80070005)

VerificationException

COR_E_VERIFICATION

WeakReferenceException

COR_E_WEAKREFERENCE

Discussion

Handling exceptions generated by COM/COM+ components involves the following two steps:

  1. Handle any specific exceptions that the .NET Common Language Runtime maps the COM/COM+ HRESULTs in which you're interested. The table in the Discussion section lists the standard HRESULT values returned by COM/COM+ objects and the .NET exceptions classes to which they are mapped.

  2. Handle any user-defined exceptions that are unique to a specific COM/COM+ component by trapping the COMException exception. The COMException class reflects COM/COM+ HRESULTs that have no mapping to managed exceptions.

The following code fragment illustrates this handling of COM/COM+ exceptions:

try
{
    CallCOMMethod( );
}
catch (UnauthorizedAccessException uae)
{
    // Handle COM/COM+ access exceptions here
}
catch (System.Runtime.InteropServices.COMException ce)
{
    // Handle user-defined COM/COM+ exceptions here
}
catch (Exception e)
{
    // Handle all other exceptions here
}

See Recipe 5.9 for more information on handling user-defined HRESULTs.

See Also

See Recipe 5.9; see the "Error Raising and Handling Guidelines," "HRESULTs and Exceptions," and "Handling COM Interop Exceptions" topics in the MSDN documentation.

    [ Team LiB ] Previous Section Next Section