Forum Discussion

twelve's avatar
twelve
Occasional Contributor
14 years ago

Exception handling

Hi,


how can exceptions that are raised by Testcomplete be correctly handled?


At sporadic intervals the execution of my Jscript testscript is interrupted and Testcomplete displays a modal dialog box: 

"An exception occurred in the "Foo" unit at line 12: This operation returned because the timeout period expired."

The test execution is stopped, I have to acknowledge this dialog with OK and then I have to restart the test execution from scratch.


This usually happens while trying to use the slice() method on the caption of a component. The application puts the system under a heavy load, so maybe the component just isn't ready at that time, I don't know. My question is: How can I tell Testcomplete to just move on? I don't want my whole test run to be stopped just because a caption cannot be read.


I tried to put a try-catch block around the calls in Foo:


funcation Foo()

{

  try

  {

    // do stuff

  }

  catch(exception)

  {

    Log.Error("Exception", exception.description);

  }

}




However now a) the test run is still stopped and b) I don't get any information about why the test stopped anymore. "exception.description" is empty!

How can a) and b) be solved, so that Testcomplete writes whatever problem it encounters to the log and then continues with the test run?


Thanks!


The project options look like this:

6 Replies


  • Hi,





    It is impossible to make TestComplete not to stop execution since this error is displayed by the Microsoft JScript scripting engine. TestComplete cannot prevent the engine from being stopped.





    The solution with the 'try..catch' block is the best one and it should work. Your test code works fine for me. Here is my modification:

    function Foo()

    {

      try

      {

        throw new Error(-1, "test exception");

      }

      catch(exception)

      {

        Log.Error("Exception", exception.description);

      }

      Log.Message("OK");

    }






    Please let me know if this does not work for you.
  • twelve's avatar
    twelve
    Occasional Contributor

    Hi,


    that code works but the following does not:


    function Foo()

    {

      try 

      { 

        throw new Error(-1, "test exception");

        // do something that triggers an error of the Jscript engine, eg a timeout

      } 

      catch(exception) 

      { 

        Log.Error("Exception", exception.description); 

      } 

      Log.Message("OK"); 

    }



    You wrote that it is impossible to tell Testcomplete to continue test execution after an error of the Jscript engine, so the statement "Log.Message("OK");" will never be executed. That makes unattended tests difficult, but "impossible" sounds as if there's currently no way to solve that problem. 


    However there's another problem: The try-catch block now correctly catches the "test exception" but it also eats the Jscript error statement. That means when the error of the Jscript engine occurs, test execution just stops and nothing happens anymore. There is no error dialog, no error details and no line number where the error happened. All I get is the word "Exception" in the log, which is not really helpful. On the other hand, if I leave out the try catch block, I get all the details about the Jscript error in a popup dialog, but then the Testcomplete error aren't catched anymore either.


    Is there a way to configure the catch statement so that it either only catches exceptions raised by Testcomplete and not by the Jscript engine, or to make it display some details about the Jscript error?


    Thanks.


  • Hi,





    that code works but the following does not:


    The only difference with my code is the line below:

        // do something that triggers an error of the Jscript engine, eg a timeout


    Could you please decrypt this comment so I can understand what kind of error you are getting?





    You wrote that it is impossible to tell Testcomplete to continue test execution after an error of the Jscript engine, so the statement "Log.Message("OK");" will never be executed. That makes unattended tests difficult, but "impossible" sounds as if there's currently no way to solve that problem.


    The solution is to use the 'try..catch' block.





    However there's another problem: The try-catch block now correctly catches the "test exception" but it also eats the Jscript error statement. That means when the error of the Jscript engine occurs, test execution just stops and nothing happens anymore. There is no error dialog, no error details and no line number where the error happened. All I get is the word "Exception" in the log, which is not really helpful. On the other hand, if I leave out the try catch block, I get all the details about the Jscript error in a popup dialog, but then the Testcomplete error aren't catched anymore either.


    The description of the exception is posted to the Remarks pane. Just select the corresponding error message in the test log, and you will see the description of the exception in Remarks. If you need to get more information, just post this information to the call of the Log.Error method. You can find information on the properties of the Error object in this MSDN Library article.





    Is there a way to configure the catch statement so that it either only catches exceptions raised by Testcomplete and not by the Jscript engine, or to make it display some details about the Jscript error?


    TestComplete never raises exceptions. The tool can post errors to the log. Some kind of errors can be handled in a specific way, and you can set this up in the Playback group of the project properties editor.
  • twelve's avatar
    twelve
    Occasional Contributor

    Hi David,


    > Could you please decrypt this comment so I can understand what kind of error you are getting?


    The decrypted version is: Any statement, that triggers a timeout error of the Jscript engine. I can post my code, but that would be of little help since you don't have the tested application and the error doesn't occur every time. I cannot reproduce it at will.


    I'll try to explain the problem again. 


    1. Sometimes during test playback, when the system is under a heavy load, the following error message appears in Testcomplete: "An exception occurred in the "Foo" unit at line 12: This operation returned because the timeout period expired."

    This error message is displayed in a popup window. After that, the test playback is stopped and I have to rerun the test from scratch. That was my first problem, but you said that this cannot be avoided and that Tescomplete always stops on these kind of errors. Ok.


    2. Now, when I add a try-catch block around the calls to the Foo function, then the test playback is still stopped if the above mentioned timeout error occurs, however no pop up window appears anymore and the details of the error are not written to the log either. The description of the exception is NOT posted to the remarks pane. Testcomplete simply stops the test playback and that's it.


    Using "catch(exception) { Log.Error("Exception", exception.description); }" only results in the string "Exception" written to the log, and the remarks are empty.

    Also the call stack pane shows the line number of the catch statement and not the line number where the actual error occured.


    This is a problem, because I cannot find out afterwards what went wrong during test playback. All I see is that an exception occured, but I don't know which and where.


    I hope that this is understandable, if not feel free to ask.


  • Hi,





    Usual JScript exceptions can be handled with the 'try..catch' block without any problems. The error you get can be caused not only by the JScript engine natively, and therefore, it is possible that is does not contain a description. To check this, use the Evaluate dialog. Set a breakpoint within the 'catch' block:





        Log.Error("Exception", exception.description); 





    When the execution is stopped at this line, open the Evaluate dialog (Ctrl-F12), type 'exception' in the 'Expression' field, and click 'Evaluate'. When the '[Object]' text appears in the 'Results' box, click 'Inspect' to see methods and properties of the 'exception' object. Please let me know your results.





    A possible solution to the 'This operation returned because the timeout period expired.' error you've faced is increasing the value of the "Method invoke timeout" setting to 10000 ms. To learn about this setting, please see the Project Properties - General Open Applications Options help topic.





    Let me know whether this helps.