This course shows you how to catch, throw, and tell apart JavaScript errors, and find what went
wrong. Python’s try looks the same in JavaScript, but JavaScript throws less often on bad
data, so code that waits for an error to reject bad data lets it slip through, and its catch
takes every error, not only the ones you meant to handle. Your own error classes, chaining errors
and a real debugger come later on the path, after classes.
Your project is to port a food bank’s donation import, the Python script its volunteer treasurer
runs, to JavaScript. Here’s a first taste. Her script, donations.py, totals the good rows of
October’s export. Here are the parts that check each amount:
def parse_amount(text):
amount = float(text)
if amount <= 0:
raise ValueError(f"amount must be positive, got {amount}")
return amount
def main():
# ... reads the rows of the export ...
total = 0
rejected = []
for line, row in enumerate(rows[1:], start=2):
date, donor, amount_text = row
try:
amount = parse_amount(amount_text)
except ValueError as err:
rejected.append((line, donor, str(err)))
else:
total += amount
# ... prints the report ...
Run as python donations.py october.csv, it rejects four rows, each with a reason:
6 rows, 4 rejected, total £125.00
line 3 (D-102): could not convert string to float: 'n/a'
line 4 (D-117): could not convert string to float: ''
line 5 (D-104): could not convert string to float: '1,250.00'
line 6 (D-105): amount must be positive, got -40.0
Here’s its JavaScript port, line by line, hook.js:
import { readFile } from "node:fs/promises";
function parseAmount(text) {
const amount = Number(text);
if (amount <= 0) {
throw new ValueError(`amount must be positive, got ${amount}`);
}
return amount;
}
const text = await readFile(process.argv[2], "utf8");
const lines = text.trim().split("\n");
let total = 0;
const rejected = [];
for (const [index, line] of lines.slice(1).entries()) {
const [date, donor, amountText] = line.split(";");
try {
total += parseAmount(amountText);
} catch (err) {
rejected.push({ line: index + 2, donor, reason: err.message });
}
}
console.log(`${lines.length - 1} rows, ${rejected.length} rejected, total £${total.toFixed(2)}`);
for (const entry of rejected) {
console.log(` line ${entry.line} (${entry.donor}): ${entry.reason}`);
}
Run the same way, with node hook.js october.csv, it prints this, without crashing:
6 rows, 2 rejected, total £NaN
line 4 (D-117): ValueError is not defined
line 6 (D-105): ValueError is not defined
The total is NaN: not a number. Two rows are rejected, not four: the other two went into the
total. Both reasons say ValueError is not defined, which isn’t about the amount. You’ll see why
in this course. Python’s try looks the same in JavaScript, but JavaScript throws less often on
bad data, and its catch takes every error.
That treasurer is Joan. Her export has rows typed by hand, and her total goes to the food bank’s trustees. The food bank’s tools are moving to JavaScript, so you’ll port her script.
By the end of this course, you’ll be able to:
- Catch errors with
tryandcatch, and check for the problems JavaScript doesn’t throw on - Throw your own errors, with messages that say what went wrong
- Catch only the errors you expect, and handle a missing file
- Find out what went wrong from a stack trace and from your logs
- Port the whole donation import to JavaScript
You only need to know how to catch exceptions in Python with try, except, else, and
finally, raise errors, and read tracebacks. In JavaScript, you only need to know how to write a
small program with functions, arrays, objects, loops, and modules, read a file with
await readFile(), and run it with node.
A current version of Node.js and of Python is fine: the examples use Node 26 and Python 3.15.
The course mixes short videos and text lessons, with exercises where you write the code yourself. Every video has a summary below it. A project puts the donation import together, and a quiz and a summary close the course.
Next up: Catching Errors With try and catch, a text lesson.