try...catch Blocks

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

β–Ά Do the exercises

Exercise 1 β€” Catching a Crash Before It Stops the Program

By default, when a runtime error happens, JavaScript stops the entire program immediately. Any line of code after the crash never gets a chance to run:

const entries = [{ name: "Zap" }, undefined, { name: "Aquachu" }]; console.log(entries[0].name); console.log(entries[1].name); // error! console.log(entries[2].name); // never reached

Because `entries[1]` is `undefined`, line 4 crashes β€” and `"Aquachu"` on line 5 is never printed! A `try...catch` block fixes that. You place the risky code inside `try`. If it crashes, JavaScript jumps straight into `catch`, runs whatever is inside it, and then continues with the rest of the program:

const entries = [{ name: "Zap" }, undefined, { name: "Aquachu" }]; try { console.log(entries[1].name); } catch (error) { console.log("Could not read that entry"); } console.log(entries[2].name);

This time, `"Aquachu"` still prints! The crash was caught, handled with a message, and the program survived.

Exercise 2 β€” Anatomy of try...catch

try { // risky code } catch (error) { // runs only if the try block crashed } // the program continues here either way

1. The `try` block holds the code that might fail. 2. The moment a line inside `try` crashes, the rest of the `try` block is skipped. 3. The `catch (error)` block receives the error object and runs its recovery code. 4. If nothing crashes inside `try`, the `catch` block is never entered. Watch point 2 in action β€” the line after the crash inside `try` never runs:

try { console.log("Before the crash"); const bad = undefined; console.log(bad.name); console.log("After the crash: never printed"); } catch (error) { console.log("Caught!"); } console.log("Program continues");

Exercise 3 β€” Reading the Caught Error

When a crash happens inside `try`, JavaScript passes an error object into `catch (error)`. That object has two useful properties you can read: 1. `error.name` β€” The error family (such as `"TypeError"` or `"RangeError"`). 2. `error.message` β€” The detailed description of what went wrong.

try { const bad = undefined; console.log(bad.name); } catch (error) { console.log("Type: " + error.name); console.log("Details: " + error.message); }

This lets your program react intelligently to a problem: you can log `error.message` for debugging while showing a friendly message to the player.

Exercise 4 β€” Quiz: What Runs?

What does this code print?

try { console.log("A"); undefined.x; console.log("B"); } catch (error) { console.log("C"); } console.log("D");
Options
β€’ A, C, D
β€’ A, B, C, D
β€’ A, D
β€’ A then a crash

Hint : A crash skips the rest of the try block and jumps to catch.

Exercise 5 β€” Quiz: No Crash

What does this code print?

try { console.log("A"); } catch (error) { console.log("B"); } console.log("C");
Options
β€’ A, C
β€’ A, B, C
β€’ B, C
β€’ A only

Hint : The catch block only runs when something went wrong.

Exercise 6 β€” Protect the Read

Wrap the risky line so the program survives and prints the fallback.

Hint : One keyword opens the protected area, the other names what runs when it fails.

Exercise 7 β€” Keep the try Block Small

Remember that as soon as one line in `try` crashes, every remaining line in that `try` block is skipped. Because of this, you should avoid wrapping your whole program in one giant `try`. Instead, wrap only the specific operation that might fail β€” especially inside a loop:

const entries = [{ name: "Zap" }, undefined, { name: "Aquachu" }]; entries.forEach((entry, index) => { try { console.log(index + ": " + entry.name); } catch (error) { console.log(index + ": broken entry"); } });

By placing a small `try...catch` inside `.forEach()`, each item gets its own safety net: the broken entry at index `1` is reported, and the loop still processes `"Aquachu"` at index `2`!

Exercise 8 β€” Report the Type

Print the error's type and its description from inside the catch block.

Hint : The caught object carries the family in one property and the details in another.

Exercise 9 β€” Quiz: The Caught Object

Inside `catch (error)`, which property holds the description of what went wrong?

Options
β€’ error.message
β€’ error.name
β€’ error.text
β€’ error.reason

Hint : One property is the type, the other is the explanation.

Exercise 10 β€” try...catch Cheat Sheet

Match each element with its role.

Items to match
β€’ try { }
β€’ catch (error) { }
β€’ error.name
β€’ Lines after the crash inside try
Definitions
β€’ The code that might fail
β€’ Runs only after a crash
β€’ The error's type
β€’ Skipped

Exercise 11 β€” Resilient Loader

The battle screen must load every entry it can, report the broken ones, and never crash.

Step 1: `describe(entry)` returns `"name (Lv.X)"` from an entry object. It does no checking of its own. Step 2: loop over `entries`. For each one, try to print `describe(entry)`. If it fails, print `"Entry N failed: type"` with the position and the error's type, then keep going. Step 3: count the successes and the failures, and print `"Loaded: N, failed: M"` at the end.

Expected output:

Zap (Lv.4) Entry 1 failed: TypeError Aquachu (Lv.12) Entry 3 failed: TypeError Loaded: 2, failed: 2
Starter code
const entries = [ { name: "Zap", level: 4 }, undefined, { name: "Aquachu", level: 12 }, null ]; // Write your code here
Back to the course