473,624 Members | 2,232 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 6025
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: "EOleSysErr or 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.c om/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
30284
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, it quitted with core dumped. %test
7
5988
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
5
2219
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
3474
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
3708
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
3061
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 block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
3
1842
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 a mehtod called ExceptionGenerator that will generate a run-time exception. The exception is caught in GenericGridEvent, but it isn't caught in InitializeComponenet (which doens't suprise me). I can't figure out where to catch an error that may...
2
1932
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 DataSet(); string strID = ds.Tables.Rows.ToString(); }
11
1989
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 more specific. I can't find any property of the exception object that tells me WHICH one it is. TIA,
32
6114
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 understand Finally runs whether an error was caught or not, I haven't found a use for finally yet.
0
8242
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8681
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8488
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
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 we have to send another system
2
1488
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.