Forum Discussion

gus's avatar
gus
Contributor
9 years ago

What's wrong with this throw statement in JScript?

See the following code, copied from the else block of an if statement in a JScript script:

 

var err = new Error();
err.number = 1;
err.name = "Could not close running Foo instance";
Log.Error(err.name, err.message, pmHigher, null, Sys.Desktop.Picture(), GetInfrastructuralLogID());
throw err;

I receive an Unspecified error during script execution when control transfers to the throw statement. The only extra information is that the error is located as column 1 (of this sample code). I saw such throw techniques used in TC help. What's wrong with mine?

  • I believe your code works as you intended. However keep this in mind:

     

    In TestComplete, if a script calls an object method that is declared in another unit, and this method throws an exception, the exception cannot be caught and handled by the try {} block in the main script. Keep this in mind when developing object-oriented test scripts using native JScript functionality.

     

    Source: https://support.smartbear.com/viewarticle/70308/

     

    Check the example code under header "Using Objects Defined in Other Units".

     

    I try to avoid exceptions and instead use event handlers because of this.

     

     

  • NisHera's avatar
    NisHera
    Valued Contributor

     

    the structure of code is not correct.

    first you should throw error and do something with that...

    once you throw error it go as a run time error witch has to handle

     

    I would rather confined to Log.error (it would not crash test ) which gives a TC error and run rest of test

    Or throw error withing try catch statement and in catch Log the error

     

    eg

    function abcd(){
    var err = new Error();
    err.number = 1;
    err.description = "Could not close running Foo instance";
    try
    {
      throw err;
    }
    catch (err)
    {
    Log.Error(err.number, err.description);
    }}

     

    • gus's avatar
      gus
      Contributor

      Hi,

       

      Thanks for the detailed explanation! Understanding that the practice applied is not correct, I'd like to ask to suggest a pattern for the case when I want to let the caller of a routing be able to handle errors in try catch blocks.

       

      function foo() {
        try {
           // do something
           bar();
        } catch (x) {
           // do something when bar() throwed an exception
        }
      )
      
      funtion bar() {
        // do something
        if (someCondition) {
          throw new Error(...);
        }
      }

      What I'd like is foo() to be able to catch the signal generated in bar(). Will the throw statement in bar() result in runtime error even if it is handled above in the call stack?

      • k_de_boer03's avatar
        k_de_boer03
        Contributor

        I believe your code works as you intended. However keep this in mind:

         

        In TestComplete, if a script calls an object method that is declared in another unit, and this method throws an exception, the exception cannot be caught and handled by the try {} block in the main script. Keep this in mind when developing object-oriented test scripts using native JScript functionality.

         

        Source: https://support.smartbear.com/viewarticle/70308/

         

        Check the example code under header "Using Objects Defined in Other Units".

         

        I try to avoid exceptions and instead use event handlers because of this.