Forum Discussion

ameett's avatar
ameett
Contributor
13 years ago

Handling Microsoft JScript runtime error

Hello,



How to handle Microsoft JScript runtime errors in the scripts?



e.g. following code -

function demo()

{

 


var a=2


var b=3


 


d=c+a+b


}



After executing above code I got following error-

An exception occurred in the "Unit3" unit at line 8:

Microsoft JScript runtime error

'c' is undefined



Now the script is paused and is waiting for user Input. On clicking OK button on the message box the script/test stops executing.

But what if, we want to run more that one tests in sequence.If such error occurs the next Test in line will not be executed.



Thanks in Advance,

Ameett


  • AlexeyK's avatar
    AlexeyK
    SmartBear Alumni (Retired)

    Ameett,


    To handle exceptions in JScript, use the try... catch blocks:




    function demo()

    {

      var a=2;

      var b=3;

     

      try

      {

         d=c+a+b;

      }

      catch(e)

      {

         // Handle exception

         Log.Message("Error: " + e.name, e.description);

      }

    }


    The try... catch statement works only for scripting errors. It will not help if an error occurs in a call to a method or property of a test object, for instance, if a click on a button fails. In this case, the method of the property will not raise an exception, but will post an error message to the test log.


    By default, this error message will also cause your test to stop. This happens, because by default, the Stop on error property of your project is enabled. When this property is enabled, TestComplete stops the test run when it detects an error message posted to the log.

    To disable the property, right-click your project in the Project Explorer panel and select Edit | Properties from the context menu. This will invoke the project editor and activate its Properties page. Select the Playback category from the tree on the left. You will see the Stop on error property on the right.


    The Stop on error property used alone (without try…catch) will not protect you from stopping on script exceptions as the script engine cannot work after an unhandled exception occurs.

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi,



    This is correct behaviour.

    OnLogError is called only for the 'logical' test errors (e.g. attempt to use an object or its property that does not exist).

    In order to handle native script run-time errors (e.g. use of the undeclared variable or attempt to divide by zero) the native exception-handling means of the given script language must be used (e.g. try/catch or On Error...)
  • ray_mosley's avatar
    ray_mosley
    Frequent Contributor
    I was attempting to use try...catch...finally to catch a condition in menu items where builds are different. For example, one build application (call it build A) has:

        Reference|ICD Search

        ReferenceItemB

        ...

    where the other build  (call it build B) has

        Reference|ICD-9 Search

        Reference|ICD-10 Search

        Reference|ItemB

        ...



    I do not have Stop on Error enabled, but do have the Pause test execution on posting an error selected.



    how can I use try catch finally to know which menu items I am processing?



    Thanks. Below is what I have tried...

    This code runs on Build B (i.e., expected results from try section) but fails on build A (errros in log but still said PSP candidate, which is wrong).



    function Test1()

    {

      //Preconditions: EMDS running; Bill started

      //

      //use try..catch..finally to determine code for RC or PSP

      //try to select Bill Reference->ICD-9 Search

      //works on PSP

      //fails on RC

      try

      {

        //try to select Bill Reference->ICD-9 Search

        Aliases.topsBill.fmBillMain.MainMenu.Click("Reference|ICD-9 Search...");

        Aliases.topsBill.fmICDSearch.sbICDExit.Click(25, 15);

        //try to select Bill Reference->ICD-10 Search

        Aliases.topsBill.fmBillMain.MainMenu.Click("Reference|ICD-10 Search...");

        Aliases.topsBill.fmICD10Search.sbICDExit.Click(26, 21);

        Log.Message("MUST BE PSP Candidate - Found ICD-9 Search & ICD-10 Search");

      }

      catch(errorObj)

      {

        Log.Message("MUST BE RC Candidate - Error: " + errorObj.name, errorObj.description);

        //must be RC candidate

        Log.Message("MUST BE RC Candidate - Found ICD Search");

      }

      finally

      {

        Log.Message("END OF TEST");

      }









  • rameshrc's avatar
    rameshrc
    New Contributor
    When Microsoft JScript runtime error comes, I expected GeneralEvents_OnLogError to be called. But it did not. It went directly to GeneralEvents_OnStopTest. Although I could see Error log posted in the log file. Is there any reason?

    TC10.40 version