JavaScript – Multiple Catch Clauses

In JavaScript 1.5, the try/catch statement has been extended to
allow multiple catch clauses. To use this feature, follow the name of
the catch clause parameter with the if keyword and a conditional

try { 
    // multiple exception types can be thrown here 
    throw 1;
catch(e if e instanceof ReferenceError) {
    // Handle reference errors here
catch(e if e === "quit") {
    // Handle the thrown string "quit"
catch(e if typeof e === "string") {
    // Handle any other thrown strings here
catch(e) {
    // Handle anything else here
finally {
    // The finally clause works as normal

When an exception occurs, each catch clause is tried in turn.
The exception is assigned to the named catch clause parameter, and the
conditional is evaluated. If true, the body of that catch clause is
evaluated, and all other catch clauses are skipped. If a catch clause
has no conditional, it behaves as if it has the conditional if true, and it is always triggered if no
clause before it was triggered. If all catch clauses have a
conditional, and none of those conditionals are true, the exception
propagates uncaught. Notice that since the conditionals already appear
within the parentheses of the catch clause, they are not required to
be directly enclosed in parentheses as they would be in a regular
if statement.

Comments are closed.