Share via


Troubleshooting Exceptions: System.Runtime.InteropServices.COMException

A COMException exception is thrown when an unrecognized HRESULT is returned from a COM method call.

Associated Tips

  • Check the ErrorCode property of the exception to determine the HRESULT returned by the COM object
    When the runtime encounters an unfamiliar HRESULT, it throws a COMException exception, which includes a public ErrorCode property that contains the HRESULT returned by the call. If an error message is available to the runtime, 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 exception. For more information, see How to: Map HRESULTs and Exceptions.

  • Disable the hosting process.
    COM is used to communicate between Visual Studio and the hosting process. Because it is used before code runs, a call to CoInitializeSecurity causes this exception to be thrown.

Remarks

The common language runtime (CLR) 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.

When passing late-bound parameters to methods of Microsoft Office objects, a COMException exception may be thrown when the objects are COM objects. The late binder assumes that such method calls involve a ByRef parameter and that the property you pass has a Set accessor. If the property does not, the .NET Framework generates a MissingMethodException exception (HRESULT CORE_E_MISSINGMETHOD). To work around this behavior, use early-bound objects or pass a variable instead of a property of the object.

See Also

Tasks

How to: Use the Exception Assistant

Concepts

Handling COM Interop Exceptions

Reference

COMException