COMException COMException COMException COMException Class

定义

当 COM 方法调用返回一个无法识别的 HRESULT 时引发的异常。The exception that is thrown when an unrecognized HRESULT is returned from a COM method call.

public ref class COMException : System::Runtime::InteropServices::ExternalException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class COMException : System.Runtime.InteropServices.ExternalException
type COMException = class
    inherit ExternalException
Public Class COMException
Inherits ExternalException
继承
派生
属性

注解

公共语言运行时转换已知的 HRESULTs 到.NET Framework 异常,使 COM 对象向托管客户端返回有意义的错误信息。The common language runtime transforms well-known HRESULTs to .NET Framework exceptions, enabling COM objects to return meaningful error information to managed clients. 异常映射到 HRESULT 还可以通过将特定的 Hresult 返回到非托管客户端适用于另一个方向。The HRESULT to exception mapping also works in the other direction by returning specific HRESULTs to unmanaged clients. 有关映射的详细信息,请参阅如何:映射 Hresult 和异常For mapping details, see How to: Map HRESULTs and Exceptions.

当运行时遇到不熟悉的 HRESULT (一个 HRESULT,缺少特定的相应异常) 时,则它引发的一个实例COMException类。When the runtime encounters an unfamiliar HRESULT (an HRESULT that lacks a specific, corresponding exception), it throws an instance of the COMException class. 此通用异常公开相同的成员的任何异常,并继承公共ErrorCode属性,其中包含相应的 HRESULT 返回被调用方。This all-purpose exception exposes the same members as any exception, and inherits a public ErrorCode property that contains the HRESULT returned by the callee. 如果一条错误消息可供运行时 (来自IErrorInfo接口或ErrVisual Basic 中,或在某些情况下从操作系统的对象),该消息返回给调用方。If an error message is available to the runtime (obtained from the IErrorInfo interface or the Err object in Visual Basic, or in some cases from the operating system), the message is returned to the caller. 但是,如果 COM 组件开发人员未能包含错误信息,则运行时返回八位数的 HRESULT 代替消息字符串。However, if the COM component developer fails to include an error message, the runtime returns the eight-digit HRESULT in place of a message string. 拥有 HRESULT 使调用方确定泛型异常的原因。Having an HRESULT allows the caller to determine the cause of the generic exception.

处理 COMException 异常Handling a COMException exception

以下是一些注意事项进行故障排除COMException异常。The following are some considerations for troubleshooting a COMException exception.

检查ErrorCode属性Check the ErrorCode property
当运行时遇到不熟悉的 HRESULT,并将引发COMException异常,ErrorCode属性包括错误消息,如果一条错误消息不可用,或八位数字 HRESULT 值。When the runtime encounters an unfamiliar HRESULT and throws a COMException exception, the ErrorCode property includes either the error message or, if an error message is unavailable, the eight-digit HRESULT value. 错误消息或 HRESULT 值可以帮助你确定异常的原因。The error message or the HRESULT value can help you determine the cause of the exception.

HRESULT 值的列表,请参阅常见的 HRESULT 值For a list of HRESULT values, see Common HRESULT Values.

后期绑定自变量和 Microsoft Office 对象Late-bound arguments and Microsoft Office objects
将后期绑定自变量传递到 Microsoft Office 对象的方法时COMException对象是 COM 对象时,可能会引发异常。When passing late-bound arguments to methods of Microsoft Office objects, a COMException exception may be thrown when the objects are COM objects. 后期联编程序假定:此类方法调用涉及 ByRef 参数,并且所传递的属性具有 set 访问器。The late binder assumes that such method calls involve a ByRef parameter and that the property you pass has a set accessor. 如果该属性不是,.NET Framework 生成MissingMethodException异常 (与CORE_E_MISSINGMETHODHRESULT)。If the property does not, the .NET Framework generates a MissingMethodException exception (with a CORE_E_MISSINGMETHOD HRESULT ). 要处理此行为,请使用早期绑定对象或传递变量而不是对象的属性。To work around this behavior, use early-bound objects or pass a variable instead of a property of the object.

Visual Studio 和承载进程Visual Studio and the hosting process
COM 使用 Visual Studio 之间进行通信并承载进程COM is used to communicate between Visual Studio and the hosting process. 因为在代码运行,对的调用前使用它CoInitializeSecurity导致引发此异常。Because it is used before code runs, a call to CoInitializeSecurity causes this exception to be thrown.

在某些情况下,以管理员身份运行 Visual Studio 可能会解决此问题。In some cases, running Visual Studio as Administrator may resolve the issue. 此外可以禁用托管进程You can also disable the hosting process.

引发 COMException 异常Throwing a COMException exception

尽管可以使用COMException类,以将特定的 Hresult 返回非托管客户端,引发特定的.NET Framework 异常优于使用一般异常。Although you can use the COMException class to return specific HRESULTs to unmanaged clients, throwing a specific .NET Framework exception is better than using a generic exception. 请考虑托管客户端,以及非托管客户端可以使用您的.NET Framework 对象,并向托管调用方引发 HRESULT 是不太理解性比引发异常。Consider that managed clients as well as unmanaged clients can use your .NET Framework object, and throwing an HRESULT to a managed caller is less comprehensible than throwing an exception.

构造函数

COMException() COMException() COMException() COMException()

使用默认值初始化 COMException 类的新实例。Initializes a new instance of the COMException class with default values.

COMException(SerializationInfo, StreamingContext) COMException(SerializationInfo, StreamingContext) COMException(SerializationInfo, StreamingContext) COMException(SerializationInfo, StreamingContext)

从序列化数据初始化 COMException 类的新实例。Initializes a new instance of the COMException class from serialization data.

COMException(String) COMException(String) COMException(String) COMException(String)

用指定的消息初始化 COMException 类的新实例。Initializes a new instance of the COMException class with a specified message.

COMException(String, Exception) COMException(String, Exception) COMException(String, Exception) COMException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 COMException 类的新实例。Initializes a new instance of the COMException class with a specified error message and a reference to the inner exception that is the cause of this exception.

COMException(String, Int32) COMException(String, Int32) COMException(String, Int32) COMException(String, Int32)

使用指定的消息和错误代码初始化 COMException 类的新实例。Initializes a new instance of the COMException class with a specified message and error code.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

当在派生类中重写时,用关于异常的信息设置 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(Inherited from Exception)
GetType() GetType() GetType() GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

将异常的内容转换为字符串。Converts the contents of the exception to a string.

属性

Data Data Data Data

获取提供有关异常的其他用户定义信息的键/值对集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
ErrorCode ErrorCode ErrorCode ErrorCode

获取错误的 HRESULTGets the HRESULT of the error.

(Inherited from ExternalException)
HelpLink HelpLink HelpLink HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult HResult HResult HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException InnerException InnerException InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message Message Message Message

获取描述当前异常的消息。Gets a message that describes the current exception.

(Inherited from Exception)
Source Source Source Source

获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)
StackTrace StackTrace StackTrace StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(Inherited from Exception)

事件

SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

适用于

另请参阅