C# vs PowerShell. How to get detailed exception info?


I'm using some external library though COM interface. I have generic class for that.

Database.Connector connector = new Database.Connector();
string connectString = "srvr=nonexisthost;database=test;"; // bogus connect string
    var database = connector.Connect(connectString);
catch (COMException ex)

Trying to build a error proof logic I intentionally provoke an exception.

And I have discovered that C# COMException contains only generic info, like:

Error HRESULT E_FAIL has been returned from a call to a COM component.

while executing samey code in PowerShell results in more detailed description:

$connector = New-Object -ComObject Database.Connector
$connectString = "srvr=nonexisthost;database=test;"
$database = $connector.Connect($connectString)

Error while executing transaction with an information base server_addr=nonexisthost descr=11001(0x00002AF9): Host is unknown. line=1048 file=src\DataExchangeCommon.cpp

My question is: what should I do in order to get same error info in C# (if it is possible)?

asked on Stack Overflow Dec 26, 2018 by Olexandr Sytnyk • edited Dec 27, 2018 by Olexandr Sytnyk

1 Answer


I'm not a COM Interop expert but I'll try to answer what I know and hope it will help you.

From the managed side

If the HRESULT is recognized by the runtime (CLR), the runtime automatically creates a specific managed exception for the error (e.g. FileNotFoundException). Otherwise, the runtime creates a generic COMException object which says "I don't know what this HRESULT meaning".

If the unmanaged code provides error info, you will see it in the ErrorCode property, otherwise, you will see just the HRESULT code. You can try to search for this code (google\github) to obtain more info.

From the unmanaged side

You need to implement ISupportErrorInfo and IErrorInfo interfaces to provide more info.

So to answer your question, in C#, you can't get more info in the COMException object if this info is not provided.

For more info: COMException, Handling COM Interop Exceptions, IErrorInfo, ISupportErrorInfo, HRESULT's mapping, Common HRESULT values

answered on Stack Overflow Dec 26, 2018 by Dudi Keleti • edited Dec 26, 2018 by Dudi Keleti

User contributions licensed under CC BY-SA 3.0