Skip to main content

What does the finally block do?

The finally block in a try...catch...finally construct is part of the error-handling mechanism, which is used to run code no matter what - regardless of whether an error occurred.


Short answer

finally runs always, after try and catch, even if try had an error, a throw fired, or the function did a return.


Syntax

javascript
try { // code that can throw an error } catch (error) { // error handling } finally { // code that runs no matter what }

How it works step by step

  1. The code in try runs.
  2. If try has no error → catch is skipped → finally runs.
  3. If try has an error → catch runs → then finally.
  4. After finally, program execution continues.

Example 1. No error - finally still runs

javascript
try { console.log("try: all good"); } catch (e) { console.log("catch: error"); } finally { console.log("finally: always runs"); }

Output:

javascript
try: all good finally: always runs

Example 2. There is an error - finally runs too

javascript
try { throw new Error("Error!"); } catch (e) { console.log("catch:", e.message); } finally { console.log("finally: always runs"); }

Output:

javascript
catch: Error! finally: always runs

Example 3. finally runs even with a return

javascript
function test() { try { console.log("try"); return "From try"; } catch { console.log("catch"); } finally { console.log("finally"); } } console.log("Result:", test());

Output:

javascript
try finally Result: From try

Even if try did a return, the finally block still runs before the function exits.


Example 4. finally runs on throw

javascript
try { throw new Error("Error!"); } catch (e) { console.log("catch:", e.message); } finally { console.log("finally runs no matter what"); }

Output:

javascript
catch: Error! finally runs no matter what

Example 5. finally runs even if catch errors

javascript
try { throw new Error("Error in try"); } catch (e) { console.log("catch:", e.message); throw new Error("Error in catch"); } finally { console.log("finally still runs"); }

Output:

javascript
catch: Error in try finally still runs Uncaught Error: Error in catch

Even if catch throws again - finally still runs.


Example 6. finally can change what return does

If finally has a return, it overwrites the result from try or catch:

javascript
function test() { try { return "From try"; } finally { return "From finally"; } } console.log(test()); // "From finally"

This is considered bad practice, because it makes the function's behavior unpredictable.


Main uses

Releasing resources:

javascript
try { openConnection(); executeQuery(); } catch (e) { console.error("Error:", e); } finally { closeConnection(); // always runs }

Cleaning up temporary data Releasing locks Finishing operations regardless of the outcome


Summary

BlockWhen it runsPurpose
tryAlwaysThe code where an error can occur
catchOnly on errorException handling
finallyAlwaysCleanup, wrap-up, releasing resources

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.