cannot catch EOleException for C++ application


Hi,

I am attempting to catch an exception for trying to open a bad file name (see code below).  [This code works perfectly fine when I use a good file name.]  When it gets a bad file name, the application pulls up the "filename.exe has encountered a problem and needs to close.  We are sorry for the inconvenience." dialog box to debug or send an error report to Microsoft.  The Debug option indicates that "An exception 'Unhandled Win32 Exception has occurred in filename.exe".

No matter how I try to catch the exception, nothing works.  (I am running the executable, not using the debugger).  btw, I am using C++ Builder 6.

Any help on what I am doing wrong would be appreciated.

Thanks,
Jeff

   Variant _wordApplication;

   // Create an instance of a MS-Word object
   _wordApplication = CreateOleObject("Word.Basic");

   AnsiString goodLogFileName = LogFilename;
   AnsiString badLogFileName = LogFilename + "BadFileExtension";

   try
       {
         // Open the log file
         //         _wordApplication.Exec(Procedure("FileOpen") << LogFilename);
         //         _wordApplication.Exec(Procedure("FileOpen") << goodLogFileName);
         _wordApplication.Exec(Procedure("FileOpen") << badLogFileName);
       }
   catch(EOleException& eOleExc)
       {
         char buf[256];
         sprintf(buf, "Result: %s.\n\n%s", eOleExc.Message, "EOleException caught\n");
         ShowMessage(buf);
         return;
       }
   catch(EOleSysError& eOleSys)
       {
         char buf[256];
         sprintf(buf, "Result: %s.\n\n%s", eOleSys.Message, "EOleSysError caught\n");
         ShowMessage(buf);       
         return;
       }
   catch(Exception& eExc)
       {
         char buf[256];
         sprintf(buf, "Result: %s.\n\n%s", eExc.Message, "Exception caught\n");
         ShowMessage(buf);       
         return;
       }
   catch(...)
       {
         char buf[256];
         sprintf(buf, "Super catch caught\n");
         ShowMessage(buf);
         return;
       }
prufrock17Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You might just encounter a Win32 SEH exception. Put the above in a single function and place the call inside a SEH frame, e.g.

__try {

    CallTheCode ();

} __except ( 1) {

       char buf[256];
       sprintf(buf, "Caught SEH exception: 0x%8.8x"\n" GetExceptionCode());
       ShowMessage(buf);

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prufrock17Author Commented:

Thanks for your quick response.  This worked out perfectly for me -- I completely overlooked the fact that this was a non-C++ exception (and I didn't know how to catch it otherwise).

Thanks,
Jeff
0
jkrCommented:
I don't know if BCB offers this feature, but VC++ has a '_set_se_translator()' where you can provide a function that translates SEH exceptions into C++ exceptions, e.g. like

/*  SETRANS.CPP
 */

#include <stdio.h>
#include <windows.h>
#include <eh.h>

void SEFunc();
void trans_func( unsigned int, EXCEPTION_POINTERS* );

class SE_Exception
{
private:
    unsigned int nSE;
public:
    SE_Exception() {}
    SE_Exception( unsigned int n ) : nSE( n ) {}
    ~SE_Exception() {}
    unsigned int getSeNumber() { return nSE; }
};
void main( void )
{
    try
    {
        _set_se_translator( trans_func );
        SEFunc();
    }
    catch( SE_Exception e )
    {
        printf( "Caught a __try exception with SE_Exception.\n" );
    }
}
void SEFunc()
{
    __try
    {
        int x, y=0;
        x = 5 / y;
    }
    __finally
    {
        printf( "In finally\n" );
    }
}
void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
{
    printf( "In trans_func.\n" );
    throw SE_Exception();
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.