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

Catching Only the Errors You Expect

Catching Only the Errors You Expect

Joan’s report takes its title from her settings file. Her Python function, report_title(), has one handler, except json.JSONDecodeError:, for the error a stray comma causes. Every other exception passes through.

The port follows the Python line by line, down to the call, with JSON.parse() built in. The title line swaps the brackets for dot access, settings.report.title. Say it gets a typo, reprot, standing in for any bug you didn’t plan for.

The except line becomes catch (err), which, much like except Exception as err, receives every error. Watch out for naming the type in the header, as in catch (SyntaxError err): JavaScript stops with SyntaxError: Unexpected identifier 'err' before anything runs, because the parentheses take only a name.

So the type moves into the catch body, as an if statement around the message. A JSON parse error is a SyntaxError too, thrown while the code runs. instanceof asks, like Python’s isinstance(), whether err is that kind of error. For the stray comma, it is. Unlike isinstance(), instanceof is an operator, not a function call. Here’s the port, typo and all:

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:00 Joan’s report takes its title from her settings file. Her Python function catches only json.JSONDecodeError, the error a stray comma causes, and every other exception passes through.

00:12 You’ll port that except line and find out what happens to the errors it doesn’t name.

00:18 The port follows the Python line by line, down to the call, with JSON.parse() built in. The title line swaps the brackets for dot access. Say it gets a typo, reprot, standing in for any bug you didn’t plan for.

00:35 The except line becomes catch (err), which, much like except Exception as err, receives every error. Watch out for naming the type in the header, as in catch (SyntaxError err): JavaScript stops with an unexpected identifier error before anything runs, because the parentheses take only a name.

00:57 So the type moves into the catch body, as an if statement around the message. A JSON parse error is a SyntaxError too, thrown while the code runs.

01:07 instanceof asks, like Python’s isinstance(), whether err is that kind of error. For the stray comma, it is. Unlike isinstance(), it’s an operator, not a function call.

01:21 Make the same typo in Python, and the script stops with a KeyError traceback: its except line lets this one out. What does the JavaScript port log when you run it?

01:31 Your turn: pause the video and predict.

01:39 It logs undefined, and nothing else. No error, no trace. settings.reprot is undefined, not an error, so the error comes a step later: a TypeError as the code reads the title.

01:56 The catch caught it. The instanceof check said no. Nothing inside the if ran, so the function fell off the end, returning undefined to the last line.

02:07 Python’s except picks errors before it catches them. instanceof checks once the catch has the error, so anything left unhandled is gone.

02:18 The fix gives Python’s behavior back by hand: end the check with an else block that holds throw err, which, like Python’s bare raise, throws the same error on.

02:28 Run it again, and the slip crashes with its TypeError, as Python’s did. Fix the line its top frame points at, and both versions print October donations.

02:40 One catch takes every error, instanceof picks out the ones you expect, and a closing else throws the rest back. Next up is Handling a Missing File, a text lesson.

02:52 November’s export isn’t downloaded yet: you’ll find out how JavaScript tells a missing file apart.

Become a Member to join the conversation.