Forum Discussion

paul_grizzaffi's avatar
paul_grizzaffi
Occasional Contributor
11 years ago

Can't catch Error thrown from a method in a different file

Hello,

Using JScript, I'm throwing an Error from a method (i.e. function on an object) ; the call to this method and the try/catch are in a different file. When the Error is thrown, it is not caught by the try/catch, instead it behaves like an unhandled Error and opens a TestComplete "Microsoft JScript Runtime Error" dialog.



Interesting to note, the code DOES catch the Error if all code resides in the same file.



Am I going about this correctly? Is this a limitation/bug/etc. I need to work around? Any guidance is appreciated.



Code is below.



Thanks!!!!

Paul

------

This code lives in file named MyScript:


//USEUNIT MyOtherFile


 


function main() {


  try {


    Obj.thisMethodThrows();


  } catch (e) {


    Log.Message("caught my error");


  }


}



This code lives in file named MyOtherFile:


Obj = {


  thisMethodThrows: function() {


    throw new Error("here's an error");


  }


}


 




4 Replies

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Paul you are correct in this is a limitation in that Exceptions can only be caught cross Script Unit if the function is at Script Level


     


    For example, if Unit1 contains


     


    function scriptUnitLevel() {


    // This exception can be caught


    throw "Exception";


    }


     


    var exObject = (function(){


    return {


    objectLevel: function() {


    // This exception cannot be caught


    throw "Exception";


    }


    };


    })();


     


    then only the Exception thrown in the function scriptUnitLevel() can be caught in another Script Unit.





    The work around I use is to have the object functions return Exceptions instead of throwing them and performing type checks on the caller to determine if an Exception was thrown.


     


    I briefly outline my approach here




  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Paul you are correct in this is a limitation in that Exceptions can only be caught cross Script Unit if the function is at Script Unit level


     


    For example, if Unit1 contains


     


    function scriptUnitLevel() {


    // This exception can be caught


    throw "Exception";


    }


     


    var exObject = (function(){


    return {


    objectLevel: function() {


    // This exception cannot be caught


    throw "Exception";


    }


    };


    })();


     


    then only the Exception thrown in the function scriptUnitLevel() can be caught in another Script Unit.





    The work around I use is to have the object functions return Exceptions instead of throwing them and performing type checks on the caller to determine if an Exception was thrown.


     


    I briefly outline my approach here




  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Appologies for the apparant double post, the forum appears to have created a new post for my edit as opposed to editing the original
  • paul_grizzaffi's avatar
    paul_grizzaffi
    Occasional Contributor
    Thanks for your quick response.




    This is extremely unfortunate; I'd missed this particular note when I originally read the "Specifics of Usage" info. Between this issue and the lack of Intellisense/code-completion, TestComplete's lack of support for user-defined objects is hindering my ability to create a future-proofed framework inside of the tool.