I have a function that may throw an exception. I would like to expose that exception as if it were thrown (essentially the same UI experience, so that it shows as an exception in the browser window and console, any debug tools). But I don't want the exception to halt execution of the original function. One solution to this problem is catching and re-throwing the exception but inside a setTimeout(). That way it will be invoked later but without interrupting the function where it originally occurred.
Example:
var f = function() {
try {
// exception could throw in here somewhere
} catch (e) {
setTimeout(
function() {
throw e;
},
100
);
}
}
My question is, are there any pitfalls to this approach? (Besides the obvious, that it may delay reporting slightly?) Or is this a relatively safe and well-accepted idiom in the JavaScript community? Are there any minor improvements to be made?
Clarification: I'd like to avoid console.log and console.error because they are non-standard, and console.log does not have the same user experience as throw e.