What browsers support Conditional catch clauses?
On MDN try...catch you can find Conditional catch clauses as Non-standard feature.
try {
myroutine(); // may throw three exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
// statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
// statements to handle EvalError exceptions
} catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
Note: This functionality is not part of the ECMAScript specification.
It's supported by any modern browsers?
Google Chrome's console returned Uncaught SyntaxError: Unexpected token if
Or shoud I use:
try {
myroutine(); // may throw three exceptions
} catch (e) {
if(e instanceof TypeError) {
// statements to handle TypeError exceptions
}
else if(e instanceof RangeError) {
// statements to handle RangeError exceptions
}
else if(e instanceof EvalError) {
// statements to handle EvalError exceptions
}
else {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
}
The “try… First, the code in try {...} is executed. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch . If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err) .
You can nest one or more try statements. If an inner try statement does not have a catch -block, the enclosing try statement's catch -block is used instead. You can also use the try statement to handle JavaScript exceptions. See the JavaScript Guide for more information on JavaScript exceptions.
Some exception can not be catch(Exception) catched. Below excecption in mono on linux, should catch without parameter. Otherwise runtime will ignore catch(Exception) statment.
You can put a try catch inside the catch block, or you can simply throw the exception again. Its better to have finally block with your try catch so that even if an exception occurs in the catch block, finally block code gets executed. Finally block may not get executed in certain exceptions.
You should use the later one, as it works everywhere, quoting MDN
And here is how to do implement the same "Conditional catch clauses" using only simple JavaScript conforming to the ECMAScript specification (obviously it's more verbose, but works everywhere (I have tested on Firefox, IE and Chrome)), I have :
try {
myroutine(); // may throw three types of exceptions
} catch (e) {
if (e instanceof TypeError) {
// statements to handle TypeError exceptions
} else if (e instanceof RangeError) {
// statements to handle RangeError exceptions
} else if (e instanceof EvalError) {
// statements to handle EvalError exceptions
} else {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
}
The first one catch (e if e instanceof ..)
only works with Mozilla Firefox and gives errors on chrome and IE
Chrome
Uncaught SyntaxError: Unexpected token if
IE
SCRIPT1006: Expected ')'
On MDN try...catch you can find Conditional catch clauses as Non-standard feature.
Note: This functionality is not part of the ECMAScript specification.
What this means is that this is not part of the javascript language that all browsers and stuff have agreed upon having available. Which most certainly will mean that not all browsers will support it if any. Your best option regarding a cross-browser solution would be the second case using a switch clause. GL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With