How to Use JavaScript Try Catch Finally Statement

javascript-logo

In JavaScript try catch finally statement is a great way for handling errors that may occur in a block of code. In this article, we will learn about all three clauses.

try{
  //do some code
}catch(e){
  //handle the exception
}finally{
  //do some code
}

Simple Example of Try Catch Finally in JavaScript

The try statement has a try block, which inside we can have one or more statements. The {} are used to open and close the block they must be used even for a single statement. After the end of the try block, the catch clause or finally clause must be used, which we can have three forms for the try/catch/finally statement.

  1. try - catch
  2. try - finally
  3. try - catch - finally

The catch clause has statements that what should be done if some sort of exception (error) is thrown in the try block. In other words first, do the try block if it is not successful (throws an exception) go to the catch block and do that (handle the exception). In case when the try block doesn't throw an exception, the catch clause will be skipped.

The finally clause always executes after the try block and the catch clause.

Unconditional catch clause

If an unconditional catch clause is used, the catch block will be executed if an exception is thrown.

try {
  throw 'myException'; // generates an exception
}
catch (e) {
  // statements to handle any exceptions
  logMyErrors(e); // pass exception object to error handler
}

Unconditional Catch Clause (Source: MDN)

Here we simply have thrown an exception in the try block, and we got the exception in the catch block, which is passed to the error handler.

Note: you can use any parameter name instead of e

Conditional catch clause

If you want to handle specific exceptions, you can use more than one catch clauses. The try block can throw three types of exceptions: TypeError, RangeError, and EvalError. When an exception is thrown, an appropriate catch clause will be used to handle the exception. If the thrown exception is not specified, and the unconditional clause is found, then the exception will be handled there.

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
}

Not Recommended Conditional Catch Clause

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 - MDN.

There is a great alternative where a conditional catch clause can be used. Let's see the next example.

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
  }
}

Recommended Conditional Catch Clause

Here we have a catch clause, and inside we have an if-else statement where we check for the type of the exception that is thrown.

The finally clause

The finally clause is the last statement that will execute after the try block and catch clause.

openMyFile();
try {
  // tie up a resource
  writeMyFile(theData);
}
finally {
  closeMyFile(); // always close the resource
}

Finally Clause

Note: The finally clause will execute regardless of whether an exception is thrown

Browser support

ChromeFirefoxSafariIE/EdgeOpera
YesYesYesYesYes

I hope now you understand how the try/catch/finally works in JavaScript if you find this article useful please share it.




#javascript

Author: Aleksandar Vasilevski |

Loading...