-
April 27th, 2005, 10:06 AM
#1
how to catch a _com_error
Hello
I have the folowing try catch
try
{
//my implementation
COMAdmin::ICOMAdminCatalogPtr spCatalog("COMAdmin.COMAdminCatalog");
HRESULT hr = spCatalog->StartApplication(m_MyComPlusApp);
}
catch(_com_error)
{
}
there are case the exceptoin is indeed thrown, and I enter the catch statment but I stiil got (before entering the catch statment) an error message & sometimes the application crash
how can I prevent that from happening?
Thanks in advacne
avi123
P.S the exception/crash happens because MyComPlusApp does not exist
-
April 27th, 2005, 11:13 AM
#2
Re: how to catch a _com_error
Have you posted your actual code?
It should be something like (this is a snippet from MSDN):
Code:
catch( _com_error &e )
{
// Crack _com_error
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE( "Exception thrown for classes generated by #import" );
TRACE( "\tCode = %08lx\n", e.Error());
TRACE( "\tCode meaning = %s\n", e.ErrorMessage());
TRACE( "\tSource = %s\n", (LPCTSTR) bstrSource);
TRACE( "\tDescription = %s\n", (LPCTSTR) bstrDescription);
// Errors Collection may not always be populated.
if( FAILED( hr ) )
{
TRACE( "*** HRESULT ***" );
TRACE( LogCrackHR( hr ) );
}
...........
}
catch(...)
{
TRACE( "*** Unhandled Exception ***" );
}
-
August 3rd, 2017, 12:01 PM
#3
Re: how to catch a _com_error
Code:
Here is a working example:
// Throw _com_error exception with our own bespoke description
void DbUtil::throwComErrorExceptionWithDescription(const wstring& desc, const _com_error& e) const
{
//Attempt to create our own _com_error
ICreateErrorInfo *pcerrinfo;
IErrorInfo *perrinfo;
HRESULT hr;
hr = CreateErrorInfo(&pcerrinfo);
LPOLESTR olestr = W2OLE((wchar_t*)desc.c_str());
pcerrinfo->SetDescription(olestr);
pcerrinfo->SetGUID(e.GUID());
pcerrinfo->SetHelpContext(e.HelpContext());
pcerrinfo->SetSource(e.Source());
hr = pcerrinfo->QueryInterface(IID_IErrorInfo, (LPVOID FAR*) &perrinfo);
if (SUCCEEDED(hr))
{
SetErrorInfo(0, perrinfo);
perrinfo->Release();
_com_error elaboratedComError(e.Error(), perrinfo);
throw elaboratedComError;
}
pcerrinfo->Release();
//fall back and throw the original _com_error
throw e;
}
Here is the try and catch
Code:
try
{
....Database command.....
}
catch(const _com_error& e)
{
wstring desc( e.Description());
wstring errorMessage(e.ErrorMessage());
desc += L" : ";
desc += cmdText;
throwComErrorExceptionWithDescription(desc, e);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|