Forum Discussion

william_diaz's avatar
william_diaz
Occasional Contributor
12 years ago

Testcomplete 9.2 JScript Exception handling question.

I have the following code.



    try

    {

      throw new Error("error...");

    }

    catch (e)

    {

      Log.Message( "Exception caught");   

    }



This works as expected. and the script completes succesfully.

but if the exception is thrown in a function higher in the stack.



    try

    {

      funcA(); // funcA calls funcB() who calls funcC() who throws the exception.

    }

    catch (e)

    {

      Log.Message( "Exception caught");   

    }



The script stops and inform the user than an exception was thrown "Microsoft JScript runtime error..."

when I dismiss the dialog the script ends and the code in the catch is never executed.



if however in funcD I catch and rethrow the exception.



    try

    {

      throw new Error(errorMessage);

    }

    catch (e)

    {

      Log.Message( "Exception caught, Rethrowing: " + errorMessage );    

      throw e;

    }



I still get the dialog that halt the script but when I dismiss the dialog the execution resumes on the catch block that calls funcA. (first code posted). as one would expect. Any ideas?



Ideally I would like the stack to unwind and not get an Exception Dialog at all. even if I have to catch and rethrow, but even that code seems like a hack.



Thanks in advance.



4 Replies

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Will, here is an example of how I work-around this issue, instead of code throwing exceptions, I utilise JScripts loose typing by returning Exceptions instead


     


    /**


    * Demonstrates the Exception handling pattern to work around the Script Unit


    * isoloation issue where the method that raises the exception would normally


    * return a value


    * @method demoPatternNormallyReturnValue


    * @author Phillip Baird


    * @date 01/07/2013


    */


    function demoPatternNormallyReturnValue() {


      var result; // Result of the method call


      


      try {


        // Call method that will return Exception


        result = exceptionRaiser.raiseExcepionWithReturn( true );


        // Check to see if a result was returned. The check is done for the generic


        // BaseException as a catch all, with more specific type checking done in the


        // catch block


        if( result && ( result instanceof exceptions.BaseException ) ) {


          throw result;


        }


        


        Log.Message( "No Exception " + result ); // No exception raised


      } catch( exObj ) {


        // Check for and handle specific exception types


        if( exObj instanceof exceptions.CreateFolderException ) {


          Log.Message( exObj.exceptionString() );


        } else {


          // Handle unknown exception type


          Log.Message( "Unknown Exception" );


        }


      }


    }


     


    I have an Exception base prototype which allows me to check for instances of specific Exceptions.


     


    It's a little un-wieldly but seems to work ok for my purposes

  • karkadil's avatar
    karkadil
    Valued Contributor
    It happens when function are located in different units. In this case exceptions can not be handled.



    This is a known limitation.
  • william_diaz's avatar
    william_diaz
    Occasional Contributor
    Thanks for the reply,



    Is there a workaround? how are others handling this?
  • sorin_velican's avatar
    sorin_velican
    Occasional Contributor

     try


        {


          funcA(); // funcA calls funcB() who calls funcC() who throws the exception.


        }


        catch (e)


        {


          Log.Message( "Exception caught");   


        }


     


    should be something like 


     


     try


        {


         if ( funcA() == "exception")  


                     throw new Error(errorMessage);


         }


        catch (e)


        {


          Log.Message( "Exception caught");   


        }


     


    and in funcA() you should have something like


     try


        {


    operation1


    operation2


    ...


    operationn


     


         }


        catch (e)


        {


          return "exception"   


        }


     


    or you can use false, -1, or what value you want