MistyD
MistyD

Reputation: 17223

How do I catch and handle `_com_error`?

I currently have code such as this

IAcctMaintPtr acct(__uuidof(AcctMaint));
acct->GetAccountList(q);

Now this code requires an external application to be running otherwise after the first statement I get an exception stating

Unhandled exception at 0x7739c41f (KernelBase.dll) in myapp.exe: Microsoft C++ exception: _com_error at memory location 0x003ccefc..

I tried to catch( const std::exception& ) but that doesn't seem to work - the exception is not being caught.

Any suggestion on how I could catch this exception ?

Upvotes: 4

Views: 7083

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262919

_com_error does not derive from std::exception, but you can catch it explicitly:

try {
    IAcctMaintPtr acct(__uuidof(AcctMaint));
    acct->GetAccountList(q);
} catch (_com_error& x) {
    // Handle error in 'x'...
}

Upvotes: 5

Related Questions