Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Checking Instead of Catching

Checking Instead of Catching

Joan’s Python script checks each donation against the donor’s pledge the usual Python way, easier to ask forgiveness than permission: it tries the lookup and handles the miss. The real script adds up every row’s pledge, so the trustees can compare pledges with what came in; here, it’s one donor. For D-117, who has no pledge on file, the KeyError sends it to the except block, so it prints No pledge on file for D-117 and the total stays 0.

The line-by-line port to JavaScript wraps the lookup in try and catch (err), but it logs only NaN. A missing key isn’t an error in JavaScript: reading one gives undefined, and += adds undefined to 0, which JavaScript turns into NaN, the not-a-number value. Python’s nearest match, 0 + None, would raise a TypeError. Nothing throws, so the catch block never runs. One missing pledge makes the whole pledge total NaN, and no error says so.

Much of what raises in Python doesn’t throw in JavaScript. An index past the end of an array gives undefined, where Python raises IndexError, and 1 / 0 gives Infinity, where Python raises ZeroDivisionError. Watch out for bad text too: a try around Number() won’t catch it, because Number("n/a") quietly gives NaN, so check the result instead.

In Python, you could look before you leap, with if donor in pledges:. In JavaScript, looking first is the way, because the lookup has nothing to throw. Object.hasOwn() asks whether an object has a key. It checks only keys set on the object itself, which is what you want here: that’s all you need about it for now. The try becomes an if, and the catch becomes the else branch:

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

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

00:14 Joan’s script checks each donation against the donor’s pledge. The website’s pledges file is written out as a dictionary, to keep it short. The real script adds up every row’s pledge, so the trustees can compare pledges with what came in.

00:29 Here, it’s one donor, D-117, so the total starts at zero.

00:37 Python tries the lookup and handles the miss. A missing key raises a KeyError, and the except block prints a message. D-117 has no pledge on file, so when you run it, Python prints that message, and the total stays at zero.

00:56 Now the check in JavaScript, ported line by line. The pledges, the donor and the total carry over as you’d expect. The lookup goes inside a try block.

01:08 JavaScript’s catch takes no error type, so catch (err) stands where except KeyError stood. The message and the last line carry over too.

01:20 D-117 still has no pledge on file. Your turn: pause the video and predict what the JavaScript port logs.

01:33 It logs NaN, and nothing else. The no-pledge message never appears, so the catch block never ran. And the total isn’t zero: it’s NaN, the not-a-number value.

01:48 In the real script, one missing pledge makes the trustees’ pledge total NaN, and no error tells Joan.

01:57 A missing key isn’t an error in JavaScript. Reading one gives undefined, JavaScript’s value for nothing there. The += adds undefined to zero, and JavaScript turns the sum into NaN, the not-a-number value.

02:15 Python’s nearest match, zero plus None, would raise a TypeError. Neither step throws, so the try block finishes and the catch is skipped.

02:26 Much of what raises in Python doesn’t throw in JavaScript. An index past the end of an array gives undefined, where Python raises an IndexError.

02:38 Dividing one by zero gives Infinity, where Python raises a ZeroDivisionError.

02:44 Watch out for bad text too: a try around Number() won’t catch it, because Number() turns text like "n/a" into NaN without a word, so check the result instead.

02:57 In Python, you could look before you leap: if donor in pledges. In JavaScript, looking first is the way, because the lookup has nothing to throw.

03:09 Object.hasOwn() asks whether pledges has the donor as a key. It checks only keys set on pledges itself, which is what you want: that’s all you need for now.

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

03:32 The if lets Joan hear about a missing pledge. When it should count as zero instead, one line replaces the if and else: ?? swaps in zero for undefined, like Python’s get() with a default.

03:47 For D-117, it gives zero. Where they part: get() checks the key, and ?? checks the value. So a pledge stored as null, JavaScript’s other empty value, becomes zero too, where Python’s get() returns the stored None.

04:07 In JavaScript, check with an if when a value might be missing, or default it with ??. Keep try and catch for what really throws: parsing JSON and reading files.

04:21 Errors you throw yourself belong there too, and the next section covers them. In the next exercise, Look Up Each Donor’s Pledge, you’ll write the two pieces Joan’s pledge check needs.

04:33 One turns the pledges file’s text into pledges, and one looks up each donor’s pledge.

Become a Member to join the conversation.