handling With a message in JavaScript - ECMAScript 2021

Find this useful? Support us: Star on GitHub 6
Category: Error handling | Language: JavaScript - ECMAScript 2021

In JavaScript, the try, catch, and finally statements are used to handle errors and manage the execution of code blocks in a more controlled way.

- The try statement is used to enclose the code that may throw an exception.
- The catch statement is used to handle the caught exception.
- The finally statement is used to execute the code block regardless of whether an exception was caught or not.

Here is an example of how to use try/catch/finally with a message in JavaScript - ECMAScript 2021:

try {
  // code that may throw an exception
  const x = y + 1;
} catch (error) {
  // handle the caught exception and display the message
  console.error("Error message: " + error.message);
} finally {
  // execute this code block regardless of whether an exception was caught or not
  console.log("Try/catch/finally executed.");
}

In this example, the try block contains an expression that may throw an exception. If an exception is thrown, the catch block will handle it and display the error message using the .message property of the caught exception object. The finally block will always execute.

If no exception is thrown, the catch block will be skipped, and only the finally block will execute.

You can also nest multiple try/catch/finally blocks to handle different types of errors or to handle them at different levels. Here is an example:

try {
  try {
    // code that may throw an exception
    const x = y + 1;
  } catch (error1) {
    // handle the caught exception and display the message
    console.warn("Warning message: " + error1.message);
  } finally {
    // execute this code block regardless of whether an exception was caught or not
    console.log("Inner try/catch/finally executed.");
  }
} catch (error2) {
  // handle the caught exception and display the message
  console.error("Error message: " + error2.message);
} finally {
  // execute this code block regardless of whether an exception was caught or not
  console.log("Outer try/catch/finally executed.");
}

In this example, there are two nested try/catch/finally blocks, which handle a possible exception differently. The outer catch/finally blocks handle any other errors that might occur.