473,498 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

try and catch - OLE exception

33 New Member
Hello,

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
  1.  
  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. }
  13.  
  14.  
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,


Ras.
Jun 5 '07 #1
6 6006
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:

Expand|Select|Wrap|Line Numbers
  1. catch(EOleSysError error1){ etc...
  2.  
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
Silent1Mezzo
208 New Member
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.

Welcome
Jun 5 '07 #3
Rasputin
33 New Member
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);
  10.  

Again, any comments on this would be highly appreciated.

Ras.
Jun 6 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 ? 
  6.              EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) 
  7.     {
  8.         // No debugger is attached, so return FALSE 
  9.         // and continue.
  10.         return FALSE;
  11.     }
  12.  
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
Rasputin
33 New Member
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,

Ras.
Jun 7 '07 #6
Rasputin
33 New Member
some good references I found on the net on structured expressions:

http://www.gamedev.net/reference/programming/features/sehbasics/default.asp
http://www.microsoft.com/msj/0197/exception/exception.aspx

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?

Thanks,

Ras.
Jun 12 '07 #7

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

Similar topics

10
30243
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
7
5962
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
5
2207
by: Jacek Dziedzic | last post by:
Hi! In my main() function I have a last-resort exception construct that looks like this: int main() { try { // ... program code }
11
3453
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
3693
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
3038
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
3
1828
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call...
2
1915
by: Ralph Krausse | last post by:
I created a try/catch/finally but when an expection is thrown, the catch does not handle it... (I know this code is wrong, I want to force the error for this example) try { DataSet ds = new...
11
1971
by: l.woods | last post by:
I want to set up my CATCH for a specific exception, but I really don't know which one of the multitude that it is. I am getting the exception now with Catch ex as Exception but I want to be...
32
6082
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.