Javascript finally finally explained

Aymeric Bouzy
Mar 26 · 3 min read

Do you know about the finally block in javascript?

If you’re like me, you’ll be like: what’s the point?

How is it different from:

Documentation on MDN is very succinct:

finally_statements: Statements that are executed after the try statement completes. These statements execute regardless of whether an exception was thrown or caught.

Hum: that doesn’t really answer all the questions I have.

  1. This mentions the try statements. Will the finally statements run if an exception is thrown inside the catch block?
  2. Will the finally statements run if I have a return statement inside the try block?
  3. Will the finally statements run if I have a return statement inside the catch block?
  4. How is the code with the finally block different from the second code I wrote? If they behave the same, why do we need that addition to the language?

Maybe you already know about how the finally block behaves: or maybe you think you know, but you’re not quite sure. You can take a moment and think about it if you want.

If you want to know the difference between the 2 pieces of code above, read the answers below.

Well: they’re almost equivalent. But there is a situation where they behave differently. Did you think about the case where handleError itself throws an error? If so, good catch 👍

Let’s build ourselves a mental model of how finally behaves by trying a few pieces of code. Let’s first consider a few simple cases:

Will output hello! (and then the uncaught exception): no big surprise. Let’s test our first question: what happens when we throw an error inside a catch block?

This will output hello! (and the uncaught exception). So, indeed, finally statements do run regardless of whether the preceding blocks throw exceptions or not (and this behaviour is not restricted at all to the try block).

However, we’re trying to see if finally is really something that we need in JS. This behaviour is still not something that couldn’t be achieved without the finally syntax; here is an equivalent code:

Ok, ok, fine. I fully admit this is much harder to read than the finally block is. So the addition to the language might be worth it only for that reason.

But still, as a side note: did you even know it was possible to have multiple catch blocks? Even syntax highlighting on GitHub gist doesn’t seem to know 😅 JS syntax is amazing…

So finally gives us a nice syntax when we want to run some code regardless of exceptions thrown in the above code. But is that all? Let’s look at how finally behaves with the return statement:

Wow. That one I didn’t expect to be honest: this does print hello!. Check it by yourself if you don’t believe me 😳

Ok so:

  1. finally is really tough: you can throw, you can return… it will run nevertheless.
  2. This breaks a big assumption I had made all along: I thought “nothing runs after a return statement”. Well: nothing except finally apparently…

Contrarily to the catch block where we could write it differently without the catch block, I don’t think we can write it differently in this case (please correct me if I’m wrong!). finally is way more powerful than it initially seemed!

As expected, this behaves exactly the same if the return statement is inside a catch block:

This does print hello! , and the function returns "early".

So we can fearlessly write code of that kind:

db.disconnect() will get run eventually, in every scenario.

It will even run if db.connect() throws an exception 😅 we might want to handle this specific case differently…


It turns out finally is indeed sugar for running code regardless of exceptions being thrown inside a block ; but it is way more powerful than that since it will run even if we return inside the block!

Why do you think this is not ever mentioned in tutorials on finally? Do you know of any other unexpected behaviour of finally keyword? Let me know in the comments! And thanks for reading 🤗

If you would like other topics covered, please tell me!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade