none
Exception handling in outlook event C++ RRS feed

  • Question

  • Hi,
    I have a program that captures an event of send email. I have faced some problems along the way (mostly in the form of releasing IDispatch).

    With my best efforts i have tried to reduce the change of a crash or an unhanded exception (which causes outlook to hang). To be on the safer side i wanted to use exception handling in the form of try/catch, but it never seems to catch the exception. 

    Is there a special way of handling exception for IDispatch?

    Here is my code

    STDMETHODIMP CAppEventListener::HandleNewMail(IDispatch * pMail, VARIANT_BOOL * bCancel)
    {
    
    	BOOL CheckEmail = FALSE;
    	mSecurityManager->PutDisableOOMWarnings(TRUE); 
    	HRESULT hr = S_FALSE;
    	VARIANT result;
    	VariantInit(&result);
    	IDispatch *senderaccount=NULL;
    	IDispatch *attchments=NULL;
    	IDispatch *attchment=NULL;
    ....
    .....
    .....
    .....
    
    /// Part i release the attachment which causes problems sometimes
    
    if (attchment)
    			{
    				try
    				{
    				attchment->Release();
    				delete [] attchment;
    				attchment=NULL;
    				}
    				catch(...)
    				{
    					exit(EXIT_FAILURE);
    				}
    }

    The code never goes to the "catch" part at a time when attachment->Release() throws an unhanded exception

    Any idea what i am doing wrong here?

    Thanks

    Tuesday, September 30, 2014 1:53 AM

All replies

  • Firstly, easting an exception is never a good idea.

    Secondly, you need to get rid of

    delete [] attchment;


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, September 30, 2014 3:34 AM
  • Hi Dimity,

    Thanks for your reply.

    I'm sorry but i don't fully understand what you mean by eating an exception is never a good idea ? 

    Is there a special way of handling COM Automation exceptions?

    And i have gotten rid of the delete part

    EDIT:

    This what my code looks like now:

    try
    		{
    		attchment->Release();
    		}
    		catch(COleDispatchException* pEx)
    		{
    			
    			exit(EXIT_FAILURE);
    
    		}
    		catch(_com_error& comError)
    		{
    			
    			exit(EXIT_FAILURE);
    		}
    		catch(...)
    		{
    			
    			exit(EXIT_FAILURE);
    		}

    Still doesn't catch the exception.

    *Note -> i am forcing an exception trying to making it release an object that doesn't exist

    • Edited by Vidur Sharma Tuesday, September 30, 2014 6:38 AM Added New code
    Tuesday, September 30, 2014 3:53 AM
  • Hello Vidur,

    Calling the exit sub in the catch blocks is not a good idea. I'd suggest tell to a user the current state or log the exception details anywhere.

    See C++ Exception Handling for more information.

    Tuesday, September 30, 2014 12:17 PM
  • You only need to catch COM exceptions in case an error comes from Outlook. Trying to catch an access violation exception is a bad idea - you need to make sure your code never runs into that problem. Does your code run correctly if you avoid calling "delete [] attchment;" and check for null before calling "attchment->Release();"?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, September 30, 2014 2:08 PM
  • Hi Dimity and Eugine,

    Thank you guys for our answers

    I have fixed the problem by initializing the attachment as NULL and and checking for null before release. And also my code runs fine even with the attchment->Release(). So my code itself is okay
    The thing is now i am forcing an error in my code(i.e trying to release something that does not exist) to try and catch an exception. To see if exception handling works, in case i run into problems later on.  

    So i just wanted to know why i am unable to catch the exception even though i'm forcing it

    Thank you

    Friday, October 3, 2014 1:43 AM
  • Hello Vidur,

    > So i just wanted to know why i am unable to catch the exception even though i'm forcing it

    I'd recommend asking such questions on the Visual C++ forum instead.

    Friday, October 3, 2014 6:46 AM