CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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 ***" );
          }

  3. #3
    Join Date
    Aug 2017
    Posts
    1

    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
  •  





Click Here to Expand Forum to Full Width

Featured