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.