By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,510 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

try and catch - OLE exception

P: 33

I am new to this forum, but the activity I have noticed reading the posts helps me believe this is a good spot for wannabes programmer like me.

I am also fairly new working under a windows environment, and even newer to work with OLE objects, so apologize (and plz correct me!) for anything wrong I might say. And maybe while I am it, would anyone be able to give a comprehensive explanations of what links OLE and COMs?

To my problem now, I am trying to program an application using Borland 5 that would connect to an Excel document, do some operations and retrieve the results.

Also, I am trying to make this application a little bit robust, so before trying to open Excel, I am checking if its not already active, and only if not I open it. My code is this:

Expand|Select|Wrap|Line Numbers
  2. try{ //Try to retrieve the application, if not opened, this would normally throw an error
  3.         vMSExcel = Variant::GetActiveObject("Excel.Application");
  4. }
  5. catch(EOleSysError error1){ //Catch the error throwned when Excel is not opened
  6.         try{ //Try to open Excel
  7.                 vMSExcel = Variant::CreateObject("Excel.Application");
  8.         }
  9.         catch(EOleSysError error2){ //Excel seems not to be installed
  10.                 ShowMessage("Unable to open Excel. Please make sure you have it installed.");
  11.         }
  12. }
I have implemented this code in a button.
Now, when I compile, there are no warnings or error, but when I click the button, the problem I encouter is that the error created by GetActiveObject is not throwned (when the application is closed). Rather, a dialog box pops-up and tells it has received an error from EOleSysError and that the process is stopped. My first catch block is thus completely ignored.

I have tried to use catch(...), still without success.

Any thoughts/advices/help on this would be much appreciated,

Jun 5 '07 #1
Share this Question
Share on Google+
6 Replies

Expert Mod 5K+
P: 9,197
This code:

Expand|Select|Wrap|Line Numbers
  1. catch(EOleSysError error1){ etc...
catches a EOleSysError object. Is that correct? I would have thoughtyour would have caught an EOleSysError&.

By using your debugger, can you see what type of obect is thrown by GetActiveObject()??

Also, check out whether you need to be using structured exceptions.

Lastly, OLE (Object Linking and Embedding) is the original word for COM. Back in the old days OLE was restricted to Office applications. COM made it more universal when OLE objects were used without an Office application.
Jun 5 '07 #2

P: 208
I also want to say welcome to the forums. I'm glad there's someone knew that doesn't just want us to help them with a homework assignment.

Jun 5 '07 #3

P: 33
Thanks for the warm welcome. I'm really looking forward to improve my programming skills, and hopefully some day be able to transfer back some of my gained knowledge to this community.

Back to my problem, you are right Weaknessforcats, the error to be catched is EOleSysError&, with the ampersand, otherwise it won't even compile. I think I copied the code during a "debugging attempt", and didn't notice, sorry.

By using the debugger I am not able to figure out which type of error is thrown. When I run the program line by line (called "trace into" in Borland 5), at the time the exception is thrown a dialog pops-up saying: "Project raised exception class EOleSysError. Process stopped. Use Step or Run to continue." What would be good is that this dialog do not appear and that the error the compiler is noticing gets forwarded to the next catch block, which would handle it.

Still, I can confirm my catch block is awaiting the proper type of error, as I got this from the help files: "EOleSysError is the exception class for errors that occur during an attempt to invoke a method or property of an OLE automation object."

Finally, when you say structured exceptions, you mean the nested try-catch blocks? Well, for now I switched to a simpler version for this portion of my code, while trying to figure out the problem:

Expand|Select|Wrap|Line Numbers
  1. .
  2.         try { // checks if Excel is opened
  3.                 vMSExcel = Variant::GetActiveObject("Excel.Application");
  4.         }
  5.         catch(EOleSysError& error1) { // Excel is not opened, open it.
  6.                 vMSExcel = Variant::CreateObject("Excel.Application");
  7.         }
  8.         //Displays Excel on the screen to confirm the opening
  9.         vMSExcel.OlePropertySet("Visible", true);

Again, any comments on this would be highly appreciated.

Jun 6 '07 #4

Expert Mod 5K+
P: 9,197
Structured exceptions are a Microsoft thing. They don't use try/catch. Instead they use __try/__except. Here's a sample:

Expand|Select|Wrap|Line Numbers
  1. __try 
  2.     {
  3.         DebugBreak();
  4.     }
  5.     __except(GetExceptionCode() == EXCEPTION_BREAKPOINT ? 
  7.     {
  8.         // No debugger is attached, so return FALSE 
  9.         // and continue.
  10.         return FALSE;
  11.     }
There's no catch block. Also, there's no throw. Instead the throw is replaced by a function call to RaiseException().

So I was just wondering if you should be using structured exceptions. Microsoft code uses structured exceptions all over.
Jun 6 '07 #5

P: 33
Thanks for this advice, I will investigate this venue, it really looks to be it. Like I've already said, I'm new to programming under a Microsoft environment, so I'll first do some googling on the topic you raised before anything.

I do not have access to my compiler until next monday, but I'll try to post the results then (successful or not).

Good day,

Jun 7 '07 #6

P: 33
some good references I found on the net on structured expressions:

but this is not it for my problem. When my program is run, it effectively enters in the try or __try block but never gets to the catch(...) or except() block. Borland seems to want to handle this exception alone and pops-up a message in a dialog box, forbiding my program to continue. This error never gets a chance to be handled.

I don't know if you would still have some ideas?


Jun 12 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.