Sometimes you may need to have one or more conditional catch clauses to handle specific exceptions:
try {myroutine(); // may throw three types of exceptions} catch (e) {if (e instanceofTypeError) {// statements to handle TypeError exceptions } elseif (e instanceofRangeError) {// statements to handle RangeError exceptions } elseif (e instanceofEvalError) {// statements to handle EvalError exceptions } else {// statements to handle any unspecified exceptionslogMyErrors(e); // pass exception object to error handler }}
Guard for exceptions
JavaScript catch doesn't support any mechanism to filter errors. This limitation isn't too hard to get around: we can write a function guard():