[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

cannot catch EOleException for C++ application

Posted on 2004-11-11
3
Medium Priority
?
1,481 Views
Last Modified: 2010-05-18

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;
       }
0
Comment
Question by:prufrock17
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12557146
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
 

Author Comment

by:prufrock17
ID: 12558024

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
 
LVL 86

Expert Comment

by:jkr
ID: 12558069
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question