COMException 類別


當 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
public class COMException : System.Runtime.InteropServices.ExternalException
type COMException = class
    inherit ExternalException
Public Class COMException
Inherits ExternalException


通用語言執行時間會將已知的 Hresult 轉換成 .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. 這個全目的例外狀況會公開與任何例外狀況相同的成員,並繼承一個包含被呼叫端所傳回之 HRESULT 的公用 ErrorCode 屬性。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介面或 Visual Basic 中的 Err 物件取得,或在某些情況下從作業系統),則會將訊息傳回給呼叫者。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 物件的方法時,如果物件是 COM 物件,則可能會擲回 COMException 例外狀況。When passing late-bound arguments to methods of Microsoft Office objects, a COMException exception may be thrown when the objects are COM objects. 晚期繫結器 (Binder) 會假設這個方法呼叫包含 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_MISSINGMETHOD HRESULT)。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 類別的新執行個體。Initializes a new instance of the COMException class with default values.

COMException(SerializationInfo, StreamingContext)

從序列化 (Serialization) 資料,初始化 COMException 類別的新執行個體。Initializes a new instance of the COMException class from serialization data.


使用指定的訊息,初始化 COMException 類別的新執行個體。Initializes a new instance of the COMException class with a specified message.

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 類別的新執行個體。Initializes a new instance of the COMException class with a specified message and error code.



取得提供例外狀況之其他使用者定義相關資訊的索引鍵/值組集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(繼承來源 Exception)

取得錯誤的 HRESULTGets the HRESULT of the error.

(繼承來源 ExternalException)

取得或設定與這個例外狀況相關聯的說明檔連結。Gets or sets a link to the help file associated with this exception.

(繼承來源 Exception)

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(繼承來源 Exception)

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(繼承來源 Exception)

取得描述目前例外狀況的訊息。Gets a message that describes the current exception.

(繼承來源 Exception)

取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.

(繼承來源 Exception)

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(繼承來源 Exception)

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(繼承來源 Exception)



判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(繼承來源 Exception)

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(繼承來源 Exception)

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(繼承來源 Exception)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)

將例外狀況的內容轉換為字串。Converts the contents of the exception to a string.



當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(繼承來源 Exception)