Loading video player…

Checking Instead of Catching

Checking Instead of Catching

Joan’s Python script checks each donation against the donor’s pledge in the pledges file, by trying the lookup and handling the KeyError. D-117 has no pledge on file, so it prints No pledge on file for D-117, and the total stays 0. Ported line by line, the lookup goes inside try, and catch (err) stands where except KeyError stood:

Language: JavaScript
const pledges = { "D-101": 50, "D-102": 25, "D-106": 75 };
const donor = "D-117";
let pledged = 0;

try {
  pledged += pledges[donor];
} catch (err) {
  console.log(`No pledge on file for ${donor}`);
}

console.log(pledged);

It logs only NaN, the not-a-number value, so the catch never ran:

Language: Program Output
NaN

A missing key isn’t an error in JavaScript: reading one gives undefined, JavaScript’s value for nothing there, and += adds it to 0, which gives NaN. Neither step throws, so the catch is skipped. In the real script, one missing pledge makes the trustees’ pledge total NaN, and no error tells Joan.

Much of what raises in Python doesn’t throw in JavaScript. An index past the end of an array gives undefined, not an IndexError, and 1 / 0 gives Infinity, with no ZeroDivisionError raised. Watch out: a try around Number() catches nothing either, because Number("n/a") quietly gives NaN; check the result instead.

Python could look first too, with if donor in pledges:. In JavaScript, looking first is the way, because the lookup has nothing to throw. Object.hasOwn(pledges, donor) asks whether pledges has the donor as a key. The try becomes an if, and the catch becomes the else branch:

Language: JavaScript
const pledges = { "D-101": 50, "D-102": 25, "D-106": 75 };
const donor = "D-117";
let pledged = 0;

if (Object.hasOwn(pledges, donor)) {
  pledged += pledges[donor];
} else {
  console.log(`No pledge on file for ${donor}`);
}

console.log(pledged);

It logs what Python prints:

Language: Program Output
No pledge on file for D-117
0

The if reports a missing pledge. When it should count as zero instead, pledged += pledges[donor] ?? 0; replaces the whole if and else, like Python’s pledged += pledges.get(donor, 0), and gives 0 for D-117.

Where they part: .get() checks the key, and ?? checks the value, so a stored null, JavaScript’s other empty value, becomes 0 too, where Python’s .get() returns the stored None unchanged.

So check with an if when a value might be missing, or default it with ??. Keep try and catch for what really throws: parsing JSON, reading files, and errors you throw, which the next section covers.

Next up: Look Up Each Donor’s Pledge, an exercise, where you’ll turn the pledges file’s text into pledges and look up each donor’s pledge.

00:00 In the last lesson, a try caught the broken settings file’s error. Now you’ll port one that does its job in Python, and see what JavaScript makes of it.

00:09 Joan’s script checks each donation against the donor’s pledge in the pledges file. D-117 has none on file, so the KeyError sends Python to the message, and the total stays zero.

00:23 Ported line by line, the lookup goes inside try, and catch (err) stands where except KeyError stood. The rest carries over.

00:32 What does the port log for D-117? Your turn: pause the video and predict.

00:43 It logs only NaN, the not-a-number value. No message appears, so the catch never ran. In the real script, one missing pledge makes the trustees’ pledge total NaN, and no error tells Joan.

01:01 A missing key isn’t an error in JavaScript: reading one gives undefined, JavaScript’s value for nothing there. The += adds it to zero, and NaN is the sum.

01:13 Neither step throws, so the catch is skipped.

01:18 Arrays too: an index past the end gives undefined, not an IndexError, and one divided by zero gives Infinity, with no ZeroDivisionError raised.

01:29 Watch out: a try around Number() catches nothing either, because "n/a" quietly becomes NaN; check the result instead.

01:40 Python could look first too, with if donor in pledges; in JavaScript, looking first is the way, since the lookup has nothing to throw. Object.hasOwn() asks whether pledges has the donor as a key.

01:54 The try becomes an if, and the catch becomes the else branch. Run it: JavaScript prints the message, then zero, as Python does.

02:05 The if reports a missing pledge. To count it as zero instead, one line replaces the if and else: two question marks swap in zero for undefined, like Python’s get() with a default.

02:18 For D-117, it gives zero. Where they part: get() checks the key, and the question marks check the value. So a stored null, JavaScript’s other empty value, becomes zero too, where Python’s get() returns the stored None unchanged.

02:36 Check with an if when a value might be missing, or default it with two question marks. Keep try and catch for what really throws: parsing JSON, reading files, and errors you throw, which the next section covers.

02:52 Next, in the exercise Look Up Each Donor’s Pledge, you’ll turn the pledges file’s text into pledges and look up each donor’s pledge.

Become a Member to join the conversation.