try and catch - OLE exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rasputin
    New Member
    • Jun 2007
    • 33

    try and catch - OLE exception

    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:

    Code:
    try{ //Try to retrieve the application, if not opened, this would normally throw an error
            vMSExcel = Variant::GetActiveObject("Excel.Application");
    }
    catch(EOleSysError error1){ //Catch the error throwned when Excel is not opened
            try{ //Try to open Excel
                    vMSExcel = Variant::CreateObject("Excel.Application");
            }
            catch(EOleSysError error2){ //Excel seems not to be installed
                    ShowMessage("Unable to open Excel. Please make sure you have it installed.");
            }
    }
    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.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:

    [code=cpp]
    catch(EOleSysEr ror error1){ etc...
    [/code]

    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.

    Comment

    • Silent1Mezzo
      New Member
      • Feb 2007
      • 208

      #3
      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

      Comment

      • Rasputin
        New Member
        • Jun 2007
        • 33

        #4
        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:

        Code:
        .
                try { // checks if Excel is opened
                        vMSExcel = Variant::GetActiveObject("Excel.Application");
                }
                catch(EOleSysError& error1) { // Excel is not opened, open it.
                        vMSExcel = Variant::CreateObject("Excel.Application");
                }
                //Displays Excel on the screen to confirm the opening
                vMSExcel.OlePropertySet("Visible", true);

        Again, any comments on this would be highly appreciated.

        Ras.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Structured exceptions are a Microsoft thing. They don't use try/catch. Instead they use __try/__except. Here's a sample:

          [code=cpp]
          __try
          {
          DebugBreak();
          }
          __except(GetExc eptionCode() == EXCEPTION_BREAK POINT ?
          EXCEPTION_EXECU TE_HANDLER : EXCEPTION_CONTI NUE_SEARCH)
          {
          // No debugger is attached, so return FALSE
          // and continue.
          return FALSE;
          }
          [/code]

          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.

          Comment

          • Rasputin
            New Member
            • Jun 2007
            • 33

            #6
            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.

            Comment

            • Rasputin
              New Member
              • Jun 2007
              • 33

              #7
              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.

              Comment

              Working...