Marshal.ThrowExceptionForHR 方法

定义

用特定的失败 HRESULT 值引发异常。

重载

ThrowExceptionForHR(Int32)

用特定的失败 HRESULT 值引发异常。

ThrowExceptionForHR(Int32, IntPtr)

基于指定的 IErrorInfo 接口,用特定的失败 HRESULT 引发异常。

ThrowExceptionForHR(Int32)

用特定的失败 HRESULT 值引发异常。

public:
 static void ThrowExceptionForHR(int errorCode);
[System.Security.SecurityCritical]
public static void ThrowExceptionForHR (int errorCode);
public static void ThrowExceptionForHR (int errorCode);
[<System.Security.SecurityCritical>]
static member ThrowExceptionForHR : int -> unit
static member ThrowExceptionForHR : int -> unit
Public Shared Sub ThrowExceptionForHR (errorCode As Integer)

参数

errorCode
Int32

与所需异常相对应的 HRESULT。

属性

注解

此方法为指定的失败 HRESULT 创建异常对象。 如果 HRESULT 为 0 或正 (成功代码) ,则该方法将返回而不创建或引发异常。

请注意,如果设置了异常,该方法 ThrowExceptionForHR(Int32) 将基于当前线程的 IErrorInfo 接口返回异常。 发生这种情况时,将 errorCode 忽略参数。

某些失败 HRESULT 映射到定义的异常,而另一些则不映射。 如果 HRESULT 映射到定义的异常, ThrowExceptionForHR 请创建异常的实例并引发该异常。 否则,它会创建一个实例 COMException,使用 HRESULT 初始化错误代码字段,并引发该异常。 调用时 ThrowExceptionForHR ,它会尝试使用非托管 GetErrorInfo 函数检索有关错误的额外信息。

有关从每个 HRESULT 映射到.NET Framework中可比较的异常类的映射,请参阅“如何:映射 HRESULT 和异常”。

有时, ThrowExceptionForHR 可能会从以前的 COM 调用中返回异常。 在这种情况下,可以使用以下解决方法并作为第二个参数 () errorInfo 传递IntPtr(-1)

public static void ThrowExceptionForHR(interrorCode,IntPtrerrorInfo)

另请参阅

适用于

ThrowExceptionForHR(Int32, IntPtr)

基于指定的 IErrorInfo 接口,用特定的失败 HRESULT 引发异常。

public:
 static void ThrowExceptionForHR(int errorCode, IntPtr errorInfo);
[System.Security.SecurityCritical]
public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo);
public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo);
[<System.Security.SecurityCritical>]
static member ThrowExceptionForHR : int * nativeint -> unit
static member ThrowExceptionForHR : int * nativeint -> unit
Public Shared Sub ThrowExceptionForHR (errorCode As Integer, errorInfo As IntPtr)

参数

errorCode
Int32

与所需异常相对应的 HRESULT。

errorInfo
IntPtr

nativeint

指向 IErrorInfo 接口的指针,该接口提供有关错误的详细信息。 可以指定 IntPtr(0) 以使用当前的 IErrorInfo 接口,或者 IntPtr(-1) 以忽略当前的 IErrorInfo 接口,并仅从错误代码构造异常。

属性

注解

此方法为指定的失败 HRESULT 创建异常对象。 如果 HRESULT 为 0 或正 (成功代码) ,则该方法将返回而不创建或引发异常。

该方法 ThrowExceptionForHR 释放 errorInfo 参数,减少 IErrorInfo 接口的 COM 引用计数。

请注意,如果设置了异常,该方法 ThrowExceptionForHR 将基于当前线程的 IErrorInfo 接口返回异常。 发生这种情况时,将 errorCode 忽略参数。

某些失败 HRESULT 映射到定义的异常,而另一些则不映射。 如果 HRESULT 映射到定义的异常, ThrowExceptionForHR 请创建异常的实例并引发该异常。 否则,它会创建一个实例 System.Runtime.InteropServices.COMException,使用 HRESULT 初始化错误代码字段,并引发该异常。 该 errorInfo 参数用于检索有关错误的额外信息。

有关从每个 HRESULT 映射到.NET Framework中可比较的异常类的映射,请参阅“如何:映射 HRESULT 和异常”。

另请参阅

适用于