.net FrameWork怎样映射HRESULT和异常

COM方法通过返回HRESULT报告错误;.NET方法通过引发异常来报告错误。运行时处理两者之间的转换。.NET Framework中的每个异常类都映射到一个HRESULT。
用户定义的异常类可以指定任何适合的HRESULT。这些异常类可以通过在异常对象上设置HRESULT字段来动态更改生成异常时返回的HRESULT。有关异常的其他信息通过IErrorInfo接口提供给客户端,该接口在非托管进程的.NET对象上实现。如果创建一个扩展System.Exception,必须在构造期间设置HRESULT字段。否则,基类将分配HRESULT值。通过在异常的构造函数中提供值,可以将新的异常类映射到现有的HRESULT。

请注意,在线程上存在IErrorInfo的情况下,运行时有时会忽略HRESULT。在HRESULT和IErrorInfo不表示相同错误的情况下,可能会发生此行为。

创建新的异常类并将其映射到HRESULT

使用以下代码创建一个名为NoAccessException的新异常类,并将其映射到HRESULT E_ACCESSDENIED。

Class NoAccessException : public ApplicationException  
{  
    NoAccessException () {  
    HResult = E_ACCESSDENIED;
}  
}  
CMyClass::MethodThatThrows  
{  
throw new NoAccessException();  
}  

您可能会遇到同时使用托管和非托管代码的程序(任何编程语言)。例如,以下代码示例中的自定义封送拆收器使用。Marshal.ThrowExceptionForHR(int HResult)(int HResult)方法引发具有特定HResult值的异常。该方法查找HRESULT并生成适当的异常类型。例如,以下代码片段中的HRESULT生成ArgumentException。

CMyClass::MethodThatThrows  
{  
    Marshal.ThrowExceptionForHR(COR_E_ARGUMENT);  
}
 下表提供了从每个HRESULT到.NET Framework中可比较的异常类的完整映射。

 

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 ArrayTypeMismatchException
COR_E_BADIMAGEFORMAT or ERROR_BAD_FORMAT BadImageFormatException
COR_E_COMEMULATE_ERROR COMEmulateException
COR_E_CONTEXTMARSHAL ContextMarshalException
COR_E_CORE CoreException
NTE_FAIL CryptographicException
COR_E_DIRECTORYNOTFOUND or ERROR_PATH_NOT_FOUND DirectoryNotFoundException
COR_E_DIVIDEBYZERO DivideByZeroException
COR_E_DUPLICATEWAITOBJECT DuplicateWaitObjectException
COR_E_ENDOFSTREAM EndOfStreamException
COR_E_TYPELOAD EntryPointNotFoundException
COR_E_EXCEPTION Exception
COR_E_EXECUTIONENGINE ExecutionEngineException
COR_E_FIELDACCESS FieldAccessException
COR_E_FILENOTFOUND or ERROR_FILE_NOT_FOUND FileNotFoundException
COR_E_FORMAT FormatException
COR_E_INDEXOUTOFRANGE IndexOutOfRangeException
COR_E_INVALIDCAST or E_NOINTERFACE InvalidCastException
COR_E_INVALIDCOMOBJECT InvalidComObjectException
COR_E_INVALIDFILTERCRITERIA InvalidFilterCriteriaException
COR_E_INVALIDOLEVARIANTTYPE InvalidOleVariantTypeException
COR_E_INVALIDOPERATION InvalidOperationException
COR_E_IO IOException
COR_E_MEMBERACCESS AccessException
COR_E_METHODACCESS MethodAccessException
COR_E_MISSINGFIELD MissingFieldException
COR_E_MISSINGMANIFESTRESOURCE MissingManifestResourceException
COR_E_MISSINGMEMBER MissingMemberException
COR_E_MISSINGMETHOD MissingMethodException
COR_E_MULTICASTNOTSUPPORTED MulticastNotSupportedException
COR_E_NOTFINITENUMBER NotFiniteNumberException
E_NOTIMPL NotImplementedException
COR_E_NOTSUPPORTED NotSupportedException
COR_E_NULLREFERENCE orE_POINTER NullReferenceException
COR_E_OUTOFMEMORY or

E_OUTOFMEMORY
OutOfMemoryException
COR_E_OVERFLOW OverflowException
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 SecurityException
COR_E_SERIALIZATION SerializationException
COR_E_STACKOVERFLOW orERROR_STACK_OVERFLOW StackOverflowException
COR_E_SYNCHRONIZATIONLOCK SynchronizationLockException
COR_E_SYSTEM SystemException
COR_E_TARGET TargetException
COR_E_TARGETINVOCATION TargetInvocationException
COR_E_TARGETPARAMCOUNT TargetParameterCountException
COR_E_THREADABORTED ThreadAbortException
COR_E_THREADINTERRUPTED ThreadInterruptedException
COR_E_THREADSTATE ThreadStateException
COR_E_THREADSTOP ThreadStopException
COR_E_TYPELOAD TypeLoadException
COR_E_TYPEINITIALIZATION TypeInitializationException
COR_E_VERIFICATION VerificationException
COR_E_WEAKREFERENCE WeakReferenceException
COR_E_VTABLECALLSNOTSUPPORTED VTableCallsNotSupportedException
All other HRESULTs COMException

要检索扩展的错误信息,托管客户端必须检查生成的异常对象的字段。要使异常对象提供有关错误的有用信息,COM对象必须实现IErrorInfo接口。运行时使用IErrorInfo提供的信息初始化异常对象。如果COM对象不支持IErrorInfo,则运行时将使用默认值初始化异常对象。下表列出了与异常对象关联的每个字段,并标识了COM对象支持IErrorInfo时默认信息的来源。
请注意,在线程上存在ierorrinfo的情况下,运行时有时会忽略HRESULT。在HRESULT和IErrorInfo不表示相同错误的情况下,可能会发生此行为。

Exception fieldSource of Information from COM
ErrorCode HRESULT returned from 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 a null reference (Nothing in Visual Basic).
Message String returned from IErrorInfo->GetDescription.
Source String returned from IErrorInfo->GetSource.
StackTrace The stack trace.
TargetSite The name of the method that returned the failing HRESULT.

异常字段(如Message、Source和StackTrace)对于StackOverflowException不可用。

 

posted on 2020-07-14 08:23  活着的虫子  阅读(319)  评论(0编辑  收藏  举报

导航