The finally Clause

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Code That Must Run No Matter What

Imagine saving a game: you open a save slot, write the data, and must always close the slot afterward — whether the write succeeded or crashed. With only `try` and `catch`, you would have to duplicate the closing line in both blocks:

const data = undefined; console.log("Slot opened"); try { console.log("Writing " + data.name); console.log("Slot closed"); } catch (error) { console.log("Write failed"); console.log("Slot closed"); }

The `finally` block removes that duplication. Whatever happens inside `try` and `catch`, the code inside `finally` is guaranteed to run at the end, always:

const data = undefined; console.log("Slot opened"); try { console.log("Writing " + data.name); } catch (error) { console.log("Write failed"); } finally { console.log("Slot closed"); }

Exercise 2 — finally Runs Even Without a Crash

What if nothing crashes inside `try`? Then `catch` is skipped, but `finally` still runs!

const data = { name: "Zap" }; try { console.log("Writing " + data.name); } catch (error) { console.log("Write failed"); } finally { console.log("Slot closed"); }

Here is how the three blocks work together: 1. No crash in `try` — `catch` is skipped, then `finally` runs. 2. A crash in `try` — `catch` runs, then `finally` runs. `finally` is the ideal place for cleanup: closing what was opened, hiding a loading spinner, or stopping a timer.

Exercise 3 — finally Runs Even Before return

Here is the most surprising feature of `finally`: even if the `try` block uses `return` to exit a function, `finally` still runs right before the function hands back its result!

function save(data) { try { console.log("Saving " + data); return "saved"; } finally { console.log("Slot closed"); } } console.log(save("Zap"));

Notice the output order: `"Slot closed"` prints before `"saved"`, because `finally` ran on the way out of `save()`. Notice also that `catch` is optional here: writing `try ... finally` without `catch` is valid when you only want guaranteed cleanup.

Exercise 4 — Quiz: Order With a Crash

What does this code print?

try { console.log("A"); undefined.x; } catch (error) { console.log("B"); } finally { console.log("C"); }
Options
• A, B, C
• A, C
• A, B
• B, C

Hint : After a crash, both remaining blocks run, in order.

Exercise 5 — Quiz: Order Without a Crash

What does this code print?

try { console.log("A"); } catch (error) { console.log("B"); } finally { console.log("C"); }
Options
• A, C
• A, B, C
• A only
• C only

Hint : One block is skipped when nothing goes wrong, the other never is.

Exercise 6 — Always Close the Slot

Make the closing message print whether the write works or not.

Hint : One keyword introduces the block that always runs, and the message must match the cleanup the slot needs.

Exercise 7 — Quiz: return Meets finally

What does this code print?

function f() { try { return "done"; } finally { console.log("cleanup"); } } console.log(f());
Options
• cleanup then done
• done then cleanup
• done only
• cleanup only

Hint : The cleanup block runs on the way out, before the value reaches the caller.

Exercise 8 — Three Blocks, Three Jobs

Match each block with when it runs.

Items to match
• try
• catch
• finally
• finally with a return in try
Definitions
• Always, first
• Only after a crash
• Always, last
• Still runs before leaving

Exercise 9 — Save Slot Manager

Write a save routine that always leaves the slot in a clean state, whatever the data looks like.

Step 1: `save(slot, data)` prints `"Slot N opened"`, then inside a `try` prints `"Saved name"` using the data's name. Step 2: on failure, the `catch` prints `"Slot N: nothing to save"`. Step 3: a `finally` prints `"Slot N closed"` in every case. Step 4: call it for slot 1 with a real Codémon and for slot 2 with `undefined`, then print `"All slots processed"`.

Expected output:

Slot 1 opened Saved Zap Slot 1 closed Slot 2 opened Slot 2: nothing to save Slot 2 closed All slots processed
Starter code
// Write your function here // Use it here
Back to the course