Learn JavaScript
Error Handling
36.7
Lesson

try...catch...finally

We can use try...catch...finally to execute a code whether the try {...} block has an error or not.

In the example given below, the try {...} block has no errors. Therefore, at first, the try {...} block executes. After that, the finally {...} block executes. It skips the catch {...} block.

When the try {...} block has an error, at first, the try {...} block executes, then the catch {...} block executes and after that, the finally {...} block executes.

In the example given below, the try {...} block has an error. Therefore, all three code blocks execute.