COMException 类

当从 COM 方法调用返回无法识别的 HRESULT 时引发的异常。

**命名空间:**System.Runtime.InteropServices
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class COMException
    Inherits ExternalException
用法
Dim instance As COMException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class COMException : ExternalException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class COMException : public ExternalException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class COMException extends ExternalException
SerializableAttribute 
ComVisibleAttribute(true) 
public class COMException extends ExternalException

备注

公共语言运行库将已知的 HRESULT 转换为 .NET 异常,这使得 COM 对象能够将有意义的错误信息返回给托管客户端。HRESULT 到异常的映射也可以反方向使用,将特定的 HRESULT 返回到非托管客户端。有关映射的详细信息,请参见 如何:映射 HRESULT 和异常

当运行库遇到未知的 HRESULT(没有特定对应异常的 HRESULT)时,将引发 COMException 类的一个实例。此通用异常将公开与任意异常相同的成员,并包含一个公共 ErrorCode 属性,其中包含由被调用方返回的 HRESULT。如果运行库获得错误信息(从 IErrorInfo 接口、Visual Basic 中的 Err 对象,或某些情况下从操作系统获得),则该信息将被返回到调用方。但是,如果 COM 组件开发人员没能提供错误信息,则运行库将返回 8 位的 HRESULT 替代消息字符串。获得 HRESULT 使调用方可以确定一般异常的起因。

虽然可以使用 COMException 类将特定的 HRESULT 返回到非托管客户端,但引发特定 .NET 异常比使用一般异常要好。考虑到托管客户端与非托管客户端一样可以使用 .NET 对象,向托管调用方引发 HRESULT 就不如引发异常更利于理解。

继承层次结构

System.Object
   System.Exception
     System.SystemException
       System.Runtime.InteropServices.ExternalException
        System.Runtime.InteropServices.COMException
           System.DirectoryServices.DirectoryServicesCOMException

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

COMException 成员
System.Runtime.InteropServices 命名空间
Exception

其他资源

处理和引发异常