Catch Only What You Can Handle

In the last lesson, you watched a throw jump to catch. When JSON.parse() threw on Ruth’s hand-typed file, the rest of the try was skipped, and the catch ran instead. Now Ruth wants to use that in her season log, so a broken file gets one clear line instead of a wall of text. Most of the work is deciding which errors a catch should handle.

In this lesson, you’ll write error handling that wraps only the code that can fail, checks that the error is the one you expected, and lets every other error through. Start with Ruth’s first attempt, which puts everything in a single try, as most people would.

See What a Wide catch Hides

A friendly message only helps if it’s true. Ruth’s first attempt puts that message in place, but her code doesn’t check whether the message describes what actually went wrong. The files below are her season-log project as you left it. If you don’t have them anymore, re-create them in a season-log folder:

This package.json tells Node.js to treat the .js files as ES modules:

Language: JSON Filename: package.json
{ "type": "module" }

The first three Saturdays of the season live in runs.json:

Language: JSON Filename: runs.json
[
  { "id": 1, "runner": "Ruth Abebe", "date": "2026-04-04",
    "distance": 5, "minutes": 27 },
  { "id": 2, "runner": "Priya Nair", "date": "2026-04-04",
    "distance": 5, "minutes": 31 },
  { "id": 3, "runner": "Tomás Herrera", "date": "2026-04-04",
    "distance": 8, "minutes": 44 },
  { "id": 4, "runner": "Grace Kim", "date": "2026-04-11",
    "distance": 10, "minutes": 52 },
  { "id": 5, "runner": "Kwame Mensah", "date": "2026-04-11",
    "distance": 5, "minutes": 24 },
  { "id": 6, "runner": "Ruth Abebe", "date": "2026-04-18",
    "distance": 8, "minutes": 45 }
]

The loader in load.js reads and parses that file:

Language: JavaScript Filename: load.js
import { readFileSync } from "node:fs";

export function loadRuns(path) {
  const text = readFileSync(path, "utf-8");
  return JSON.parse(text);
}

The report in report.js formats and prints each run:

Language: JavaScript Filename: report.js
export function formatRun(run) {
  const who = `${run.runner} (${run.date})`;
  return `${who}: ${run.distance} km in ${run.minutes} min`;
}

export function printSeason(runs) {
  for (let i = 0; i < runs.length; i++) {
    console.log(formatRun(runs[i]));
  }
}

In the last video, Ruth typed Grace’s run straight into runs.json. From now on, she keeps hand-typed runs in a file of their own, new-runs.json, until she’s checked them. So delete Grace’s run from runs.json, leaving the six runs shown above.

Ruth’s next two runs, Priya’s as run 7 and Grace’s renumbered as run 8, are in new-runs.json, the copy she typed by hand. It has a comma after the last "minutes" value, which JSON doesn’t allow:

Language: JSON Filename: new-runs.json
[
  { "id": 7, "runner": "Priya Nair", "date": "2026-04-25",
    "distance": 10, "minutes": 58 },
  { "id": 8, "runner": "Grace Kim", "date": "2026-04-25",
    "distance": 5, "minutes": 26, }
]

Ruth wraps the calls in main.js in a try, with a catch that logs a friendly line:

Language: JavaScript Filename: main.js
import { loadRuns } from "./load.js";
import { printSeason } from "./report.js";

try {
  const runs = loadRuns("new-runs.json");
  printSeason(runs);
} catch (error) {
  console.log("Couldn't read the season file.");
}

The examples use Node.js 26. Run this version of main.js, and the wall of text is gone:

Language: Shell
$ node main.js
Couldn't read the season file.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.