Error Handling

Errors (exceptions) may occur both on JS-side and AS. You can handle (catch) them in your JS code as well as in AS – they are chained and passed through all the evaluation contexts. Evaluator#multiEval(String) and Evaluator#evaluate(String)wrap thrown errors into ru.hwl.svetka.EvaluatorError. You can get chained error calling getCause() method. Consider the following unit test code:

try {
    evl.multiEval("throw 'exception!'");
} catch (e : EvaluatorError) {
    assertEquals(e.getCause(), "exception!");
}

// throwingMethod() just throws new Error("Hello!")
assertTrue(evl.multiEval("try { testBean.throwingMethod(); } catch(e) { _result = (e.message == 'Hello!') }"));

try {
    evl.evaluate("testBean.throwingMethod()");
} catch (e : EvaluatorError) {
    assertEquals(e.getCause().message, "Hello!");
}

Note that throwing objects are treated differently than usual ones when being passed through JS/AS interface: they are not placed into any context, their properties are retained, and no stub methods are generated for them. In other words evaluator passes them via ExternalInterface mechanism without any processing. You have to remember that object passed from AS-side to JS and back is another object. And it’s not only another instance – it even loses its methods since it has been passed through ExternalInterface.