Forum Discussion

m_essaid's avatar
m_essaid
Valued Contributor
4 years ago

How to catch errors and exceptions

Hi,

Untill now I catch errors with the OnError event.

But I need to perform a method on "OnError", a method that is in the script.

Please consider this code :

 

procedure TestErrorException;
begin
  try
    // Goes to OnError event : not ok
    Aliases.MyExe.FormCptReciproque.PanelBas.XPBitBtnSaisieEcr.Click();
    
    //Goes to the finally : ok
    a_variable:= 0;
    
    //Goes to the finally : ok
    raise('I raise an exception');
    
  finally
    Log.Message('I need to go there in every case');
  end;
end;

 

I try to make for every case the procedure going to the finally. It works well in the case 2 and 3, but in case of not finding an object, it raises an error that trigger the OnError event.

But I still need to be into the call method, before the event to performs some steps.

Any help would be greatly appreciated

 

8 Replies

  • BenoitB's avatar
    BenoitB
    Community Hero

    If it's on intercepting not found error you can modify your onError event.

    I suggest you to look at this thread.

     

    As for me to fully control the error flow I use a mix between a global variable storing the last error object (null if no error), the try..catch syntax and the onError event.

    • m_essaid's avatar
      m_essaid
      Valued Contributor

      The problem is that I need variables that are specific to the test item, and thus available only in the test item...

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Your code should pass through the finally statement for all three cases. It might terminate immediately if you have your project's/test item's setting to stop on error, but even in this case I would expect finally statement to execute and I would consider this behavior as DelphiScript's flaw and contact Support (https://support.smartbear.com/message/?prod=TestComplete) for their confirmation (and, hopefully, fix).

     

    • m_essaid's avatar
      m_essaid
      Valued Contributor

      Hi Alex,

      Thank you for replying,

      In the OnLogError I had a Runner.Stop(True).

      I understand that with this statement TC would stop and not go to the finally.

      But I tried to remove it, and the main procedure just continues.

      I tried to put a "raise" in OnLogError and the main procedure still continues without going instantly to the finally.

      I wrote to the support...

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi  Mehdi,

         

        I tried to put a "raise" in OnLogError and the main procedure still continues without going instantly to the finally.

        I have a strong feeling that several years ago there was a breaking change in DelphiScript engine (not sure was it for purpose or not) that unhandled exception thrown in some called script unit stopped to be traced back to the caller parent one that contains exception handling structures. This is how DelphiScript functioned initially for a lot of years.

        I would really hope that guys will be able to correct this behavior while working on your case.