Marshal.ThrowExceptionForHR 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
引发具有特定故障 HRESULT 值的异常。
重载
| 名称 | 说明 |
|---|---|
| ThrowExceptionForHR(Int32) |
引发具有特定故障 HRESULT 值的异常。 |
| ThrowExceptionForHR(Int32, IntPtr) |
根据指定的 IErrorInfo 接口引发具有特定故障 HRESULT 的异常。 |
| ThrowExceptionForHR(Int32, Guid, IntPtr) |
ThrowExceptionForHR(Int32)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
引发具有特定故障 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 调用中返回异常。 在这种情况下,可以使用以下解决方法并作为第二个参数传递 IntPtr(-1) (errorInfo):
public static void ThrowExceptionForHR(interrorCode,IntPtrerrorInfo)
另请参阅
适用于
ThrowExceptionForHR(Int32, IntPtr)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
根据指定的 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 和异常。
另请参阅
适用于
ThrowExceptionForHR(Int32, Guid, IntPtr)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
public:
static void ThrowExceptionForHR(int errorCode, Guid % iid, IntPtr pUnk);
public static void ThrowExceptionForHR(int errorCode, in Guid iid, IntPtr pUnk);
static member ThrowExceptionForHR : int * Guid * nativeint -> unit
Public Shared Sub ThrowExceptionForHR (errorCode As Integer, ByRef iid As Guid, pUnk As IntPtr)
参数
- errorCode
- Int32
- iid
- Guid
- pUnk
-
IntPtr
nativeint