Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
COMException Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
COMException Class

The exception that is thrown when an unrecognized HRESULT is returned from a COM method call.

Namespace:  System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class COMException _
    Inherits ExternalException
Visual Basic (Usage)
Dim instance As COMException
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class COMException : ExternalException
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class COMException : public ExternalException
JScript
public class COMException extends ExternalException

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 How to: Map 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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker