Handling Interop Errors

In the .NET Framework, the CLR reports errors by throwing exceptions when things go wrong. In COM, HRESULTs are the avenue in which errors are reported, so the RCW needs to be able the map the HRESULT for a given error to the equivalent .NET exception.

Table 34-2 maps the standard HRESULTs in COM to their counterparts as .NET exceptions.

Table 34-2: HRESULTs to .NET Exceptions

HRESULT

.NET Exception

MSEE_E_APPDOMAINUNLOADED

AppDomainUnloadedException

COR_E_APPLICATION

ApplicationException

COR_E_ARGUMENT or E_INVALIDARG

ArgumentException

COR_E_ARGUMENTOUTOFRANGE

ArgumentOutOfRangeException

COR E ARITHMETIC or ERROR_ARITHMETIC_OVERFLOW

ArithmeticException

COR_E_ARRAYTYPEMISMATCH

Array TypeMismatchExcepti on

COR E BADIMAGEFORMAT or ERROR_BAD_FORMAT

BadImageFormatException

COR_E_COMEMULATE_ERROR

COMEmul ateExcepti on

COR_E_CONTEXTMARSHAL

ContextMarshalException

COR_E_CORE

CoreException

NTE_FAIL

Cry ptographi cExcepti on

COR E DIRECTORYNOTFOUND or ERROR_PATH_NOT_FOUND

DirectoryNotFoundException

COR_E_DIVIDEBYZERO

DivideByZeroException

COR_E_DUPLICATEWAITOBJECT

DuplicateWaitObjectException

COR_E_ENDOF STREAM

EndOfStreamExcepti on

COR_E_TYPELOAD

EntryPointNotFoundException

COR_E_EXCEPTION

Exception

COR_E_EXECUTIONENGINE

ExecutionEngineException

COR_E_FIELD ACCES S

FieldAccessException

COR E FILENOTFOUND or ERROR_FILE_NOT_FOUND

FileNotFoundException

COR_E_FORMAT

FormatException

COR_E_INDEXOUTOFRANGE

IndexOutOfRangeExcepti on

COR E INVALIDCAST or E_NOINTERFACE

InvalidCastException

COR_E_INVALIDCOMOBJECT

InvalidComObj ectException

COR_E_INVALIDFILTERCRITERIA

InvalidFilterCriteriaException

COR_E_INVALIDOLEVARIANTTYPE

InvalidOleVariantTypeException

COR_E_INV ALIDOPERATION

Invali dOperati onExcepti on

Table 34-2: HRESULTs to .NET Exceptions

HRESULT

.NET Exception

COR_E_IO

IOException

COR_E_MEMBERACCES S

AccessException

COR_E_METHOD ACCES S

MethodAccessException

COR_E_MIS SINGFIELD

MissingFieldException

COR_E_MIS SINGMANIFESTRESOURCE

MissingManifestResourceException

COR_E_MIS SINGMEMBER

Mi s singMemb erExcepti on

COR_E_MIS SINGMETHOD

Mi s singMethodExcepti on

COR_E_MULTICASTNOTSUPPORTED

MulticastNotSupportedException

COR_E_NOTFINITENUMBER

NotFiniteNumb erExcepti on

E_NOTIMPL

NotImpl ementedExcepti on

COR_E_NOT SUPPORTED

NotSupportedException

COR_E_NULLREFERENCE or E_POINTER

NullReferenceException

COR E OUTOFMEMORY or E_OUTOFMEMORY

OutOfMemoryException

COR_E_OVERFLOW

Overfl owExcepti on

COR E PATHTOOLONG or ERROR_FILENAME_EXCED_RANGE

PathTooLongException

COR_E_RANK

RankException

COR_E_REFLECTIONTYPELOAD

ReflectionTypeLoadException

COR_E_REMOTING

RemotingException

COR_E_SAFEARRAYTYPEMISMATCH

SafeArrayTypeMismatchException

COR_E_SECURITY

S ecurityExcepti on

COR_E_SERIALIZATION

S erializationException

COR E STACKOVERFLOW or ERROR_STACK_OVERFLOW

StackOverflowException

COR_E_SYNCHRONIZATIONLOCK

SynchronizationLockException

COR_E_SYSTEM

SystemException

COR_E_T ARGET

TargetException

COR_E_TARGETINVOCATION

TargetInvocationException

COR_E_TARGETPARAMCOUNT

TargetParameterCountException

COR_E_THRE AD ABORTED

Thread Ab ortExcepti on

COR_E_THREADINTERRUPTED

ThreadInterruptedException

COR_E_THREADSTATE

Thread StateExcepti on

COR_E_THREADSTOP

Thread StopExcepti on

COR_E_TYPELOAD

TypeLoadException

COR_E_TYPEINITIALIZATION

TypeInitializationException

Table 34-2: HRESULTs to .NET Exceptions

HRESULT

.NET Exception

COR_E_VERIFICATION

Verifi cati onExcepti on

COR_E_WEAKREFERENCE

WeakReferenceException

COR_E_VTABLECALLSNOTSUPPORTED

VTableCallsNotSupportedException

All other HRESULTs

COMException_

If your application needs to get extended error information and the COM object supports the IErrorlnfo interface, you can use the IErrorlnfo object to get further information about the exception. Table 34-3 describes the additional error information.

Table 34-3: COM Interop Extended Error Information

Exception Field

COM Source Information

ErrorCode

HRESULT returned from the method call

HelpLink

If IErrorInfo->HelpContext is nonzero, the string is formed by concatenating IErrorInfo->GetHelpFile and "#" and IErrorInfo->GetHelpContext. Otherwise, the string is returned from IErrorInfo->GetHelpFile.

InnerException

Always null

Message

String returned from IErrorInfo->GetDescription

Source

String returned from IErrorInfo->GetSource

StackTrace

The .NET generated stack trace for this exception

TargetSite

The method name that caused the HRESULT to be passed back to NET

Obviously, you need to include error handling in your applications, even if you are using COM Interop. There is essentially no difference in the way that you code the COM components versus .NET assemblies, so the structured exception handling in .NET should be used whenever you are writing code that has the possibility of causing an exception.

0 0

Post a comment