I do Automation Testing with JavaScript (JScript actually, but I feel like it applies to either) and am wondering if anyone knows of a way to throw an exception inside a prototyped object method and have it be caught by the outer function as shown below. MyObject will be called inside the outer function (a "TestCase" to be run on the software using a Testing software called TestComplete).
function MyObject()
{
//Object properties
}
//Inherits object properties and methods from ParentObject
MyObject.prototype = new ParentObject();
MyObject.prototype.constructor = MyObject;
MyObject.prototype.foo = function()
{
//Will try to perform some action and may throw an exception
}
In a separate file (after investigating more, this seems to be the main issue but cannot be fixed due to the overarching structure of the code/library):
function TestCase()
{
try
{
var object = new MyObject()
object.foo();
}
catch(e)
{
//Log an error
}
finally
{
//Clean up
}
}
What I am seeing now is that TestComplete 10 (my IDE/Debugger essentially) will cause a Runtime Error saying that an exception is thrown and not caught, even though the function is being called inside an external try catch. I have had this work using just other regular functions, and I need it to work this way so that if an exception is thrown, it will come out to the catch, ending the test case, and then cleaning up from the test case steps.
Thanks!
After more investigation on some of the Test Complete forums, it seems this is a known issue. So this really has nothing to do with JavaScript/JScript. This is shown in some forum posts here,
http://www.sqaforums.com/forums/automatedqa-smartbear-testcomplete/84991-useunit-exceptions.html
http://smartbear.com/forums/f74/t49620/jscript-exception-propagation-between-script-un/
However, here is a more detailed solution to what you can do to fix this problem, it is just really ugly. I had already figured this as a solution, but I wanted to know if there was a better one, as I thought this was initially a JavaScript/JScript error.
http://smartbear.com/forums/f81/t74287/handling-exceptions-in-different-units/