Where to CATCH an EXCEPTION thrown within an EVAL

Viewed 1490

The following script enables you to run a piece of javascript code. Errors are being catched by the try / catch block.

    try {
        var result = eval(script);
    } catch (e) {
        // do something meaningful
    }

However, if the variable script contains for instance an AJAX call, and this ajax call throws an exception (e.g. in the success function), this exception will NOT be catched by this try / catch block...

    // execute an AJAX request
        var script = '$.ajax(url:"/somewhere", success: function(){throw new MyException('testexception')})';
    try {
        var result = eval(script);
    } catch (e) {
        // will not be triggered...
    }

Question: how can I catch the exception thrown within the ajax request?

1 Answers
Related