COMException Class
The exception that is thrown when an unrecognized HRESULT is returned from a COM method call.
For a list of all members of this type, see COMException Members.
System.Object
System.Exception
System.SystemException
System.Runtime.InteropServices.ExternalException
System.Runtime.InteropServices.COMException
[Visual Basic] <Serializable> Public Class COMException Inherits ExternalException [C#] [Serializable] public class COMException : ExternalException [C++] [Serializable] public __gc class COMException : public ExternalException [JScript] public Serializable class COMException extends ExternalException
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
The common language runtime transforms well-known HRESULTs to .NET exceptions, enabling COM objects to return meaningful error information to managed clients. The HRESULT to exception mapping also works in the other direction by returning specific HRESULTs to unmanaged clients. For mapping details, see HRESULTs and Exceptions.
When the runtime encounters an unfamiliar HRESULT (an HRESULT that lacks a specific, corresponding exception), it throws an instance of the COMException class. This all-purpose exception exposes the same members as any exception, and includes a public ErrorCode property that contains the HRESULT returned by the callee. 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. 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. Having an HRESULT allows the caller to determine the cause of the generic exception.
Although you can use the COMException class to return specific HRESULTs to unmanaged clients, throwing a specific .NET exception is better than using a generic exception. Consider that managed clients as well as unmanaged clients can use your .NET object, and throwing an HRESULT to a managed caller is less comprehendible than throwing an exception.
Requirements
Namespace: System.Runtime.InteropServices
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: Mscorlib (in Mscorlib.dll)
See Also
COMException Members | System.Runtime.InteropServices Namespace | Exception | Handling and Throwing Exceptions