Forum Discussion

xylian's avatar
xylian
Occasional Contributor
14 years ago

Exception handling problems

Hello

I'm encountering serious issue with exception handling. I'm trying to handle such code fragment (delphi script):



try

Self.objectRefference.Clickzzzzzzz

except

Log.Message('exception caught');

end;



Obviously there is no such method as Clickzzzzzzz - my point is to prevent the test from stoping, but such code fails at this. Test stops, and the except block is not even beeing executed.



I'd appreciate any tips on that.



Regards
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Syntax errors such as unknown identifiers and such, as far as I know, don't get trapped by TestComplete's scripting engine.
  • xylian's avatar
    xylian
    Occasional Contributor
    it's just an example. But lets say, some button dissapeared from the program, but I don't want the test to stop because of that. Will the try block catch this as an exception?
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    no.  I just tried the following.



    procedure TestThis;





    var MyObject;





    begin

        try

        MyObject := Aliases.Orders.OrderFrm.VCLObject('OButton');

        MyObject.ClickButton;

        except

            Log.Message('Yada');

        end;

    end;




    The "OButton" object does not exist so I get an error indicating that it could not find the object.  These kinds of errors don't raise an exception so they don't trigger the try/accept logic.



    Now, if you're unsure as to whether or not an object will exist during running scripts and you want to test for it, I would suggest using methods like WaitVCLObject or WaitAliasChild.  Like so



    procedure TestThis;





    var MyObject;





    begin

        try

        MyObject := Aliases.Orders.OrderFrm.WaitVCLObject('OButton');

        if not MyObject.Exists then

            raise('Could not find object')

        else MyObject.ClickButton;

        except

            Log.Message('Yada');

        end;

    end;
  • xylian's avatar
    xylian
    Occasional Contributor
    I'm not exactly happy about such poor functionality of try block, but I think I could use the exist function instead. Thank you, that helps a lot.
  • xylian's avatar
    xylian
    Occasional Contributor
    I've ran into a problem trying to handle Evaluate method error. Let's say I'm usingEvaluate(path) but given path is incorrect. I wan't to catch this to avoid test from stopping.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    The try/except/finally logic only handles exceptions raised by the code, not errors generated by TestComplete's other engines.  



    As for your other question, first of all, for additional queries, please start a new thread in the future to make it easier for other users to find answers and such.



    Secondly, what sort of path are you putting into evaluate?



    I tried the following:



    procedure TestThis;





    var MyObject;





    begin

        try

        Evaluate('Unit1.TestThat');

        except

            Log.Message('Yada');

        end;

    end;




    And I correctly went into my except block because the TestThat method doesn't exist.  Could you describe in more detail what you're doing to know why the exception handling is not working?



  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    From your other thread, the items that you're evaluating with the EVALUATE method are objects in your application.  You'll run into, essentially, the same problem as when you started this thread: the existence of an object does not fire the DelphiScript exception handling.  So, you should include in your Evaluate, if you're going that route, the idea of WaitNNN where NNN is the specific method.


  • xylian's avatar
    xylian
    Occasional Contributor
    You're right. One of waitNNN function is what I need. But to call it, I need a refference to the object because it's called like TestObj.WaitNNN. To have a refference for such object I must call Evaluate which looks like this:



    Evaluate(ApplicationWindow.FullName+'.WinFormsObject(''tabControl2'')'+'.WinFormsObject(''tabPage3'')'+'.WinFormsObject(''aprButtonEdi'')'



    which will cause test stop because path is incorrect due to non existing object under it. And As you already stated, it will not cause delphi exception.
  • xylian's avatar
    xylian
    Occasional Contributor
    EDIT



    My path could look aswell like:

    Sys.Process('WindowsFormsApplication1').WinFormsObject('Form1').WinFormsObject('tabControl2').WinFormsObject('tabPage3').WinFormsObject('aprButtonEdi')


  • xylian's avatar
    xylian
    Occasional Contributor
    OK I managed to solve that thanks to your tips. Here is what I've done:




    procedure TryTest;

    var

      path, obj :OleVariant;

    begin

      path := Sys.Process('WindowsFormsApplication1').WinFormsObject('Form1').WinFormsObject('tabControl2').WinFormsObject('tabPage3'); 

      

      obj := path.WaitWinFormsObject('aprButtonEdi', 0);  

      

      if obj.Exists then

        Log.Message('true')

      else

        Log.Message('false');

      

      //Evaluate(path.FullName);

    end;


    This condition is handling what I need. Thank you very much Robert Martin