Categories
JavaScript Answers

How to add try block without catch block in JavaScript?

Spread the love

Sometimes, we want to add try block without catch block in JavaScript.

In this article, we’ll look at how to add try block without catch block in JavaScript.

How to add try block without catch block in JavaScript?

To add try block without catch block in JavaScript, we can add an empty catch or finally block.

For instance, we write

try {
  throw new Error("This won't show anything");
} catch {}

try {
  throw new Error("This will get logged");
} finally {
  //...
}

to add a catch or finally block after the try block.

Adding an empty catch would swallow the error and adding a finally block wouldn’t swallow the error but let us run code regardless of whether an exception is thrown.

Conclusion

To add try block without catch block in JavaScript, we can add an empty catch or finally block.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *