Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple catch in javascript

Tags:

javascript

People also ask

Can we use multiple catch blocks in JavaScript?

No, multiple catch blocks cannot be executed. Once first catch block is catched, it will not read the next block.

How can you catch multiple exceptions?

When catching multiple exceptions in a single catch block, the rule is generalized to specialized. This means that if there is a hierarchy of exceptions in the catch block, we can catch the base exception only instead of catching multiple specialized exceptions.

What is multiple try catch?

Java Multi-catch block A try block can be followed by one or more catch blocks. Each catch block must contain a different exception handler. So, if you have to perform different tasks at the occurrence of different exceptions, use java multi-catch block.

Can you have 2 try catch?

You cannot have multiple try blocks with a single catch block. Each try block must be followed by catch or finally.


No. That does not exist in JavaScript or EcmaScript.

You can accomplish the same thing with an if[...else if]...else inside of the catch.

There are some non-standard implementations (and are not on any standard track) that do have it according to MDN.


Try in a that way:

try {
  throw 1; 
}
catch(e) {
    if (e instanceof ReferenceError) {
       // ReferenceError action here
    } else if (typeof e === "string") {
       // error as a string action here
    } else {
       // General error here
    }
}
finally {}

There is absolutely nothing wrong with multiple if/then/else of course, but I never liked the look of it. I find that my eyes skim a bit faster with everything lined up, so I use the switch approach instead to help me skim/seek to the correct block. I've also started using a lexical scope {} to enclose case blocks now that the ES6 let command has gained popularity.

try {

  // OOPS!

} catch (error) {

  switch (true) {
    case (error instanceof ForbiddenError): {
      // be mean and gruff; 
      break;
    }
    case (error instanceof UserError): {
      // be nice; 
      break;
    }
    default: {
      // log, cuz this is weird;
    }
  }

}

This Kind of Multiple Catch we call in javascript as Conditional catch clauses

You can also use one or more conditional catch clauses to handle specific exceptions. In this case, the appropriate catch clause is entered when the specified exception is thrown. As below

try {
    myroutine(); // may throw three types of 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
}

Non-standard: But This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

Reference