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

Let an Error Climb to the Right Catch

In this video, you followed the error that checkRun() throws as it climbed out of one function after another, and you moved the catch to the level of Ruth’s season log that can do something about a bad run.

Key Points

  • One bad run still stops the whole import. Ruth moves the loop that calls checkRun() out of loadRuns(), so the import has its own step, where a bad run could be skipped. Her new importRuns() in load.js loops over the runs, checks each one with checkRun(), and keeps it. main.js sends the runs through importRuns() before printing them. Grace Kim’s run in new-runs.json has no distance, so checkRun() throws a TypeError. Nothing catches it, so the whole program stops, and even the good runs from Priya and Kwame never print.
  • Each call puts a frame on the call stack. When a function throws and there’s no try around that line, the function stops there, its frame comes off, and the error comes out at the line that called it. The error keeps climbing, one frame at a time, until a catch stops it. If nothing does, the program stops and Node prints the report.
  • The at lines in the report, read from the top, are the function that created the error, then the calls waiting under it. checkRun() creates its error right where it throws it, so here they also trace the error’s climb.
  • Catching the error inside checkRun() with a catch that logs and returns hides it. The message prints, but the call returns as it does for a good run, so importRuns() keeps Grace’s run, and the season log prints undefined km.
  • Watch out for a function that logs a problem and carries on, with a catch like that one, or with a log where a throw should be. The code that could respond never hears about it.
  • The code that can skip a run is the loop in importRuns(). A small try there holds the check and the push. When checkRun() throws, the error climbs one frame to the call inside that try. The catch checks the error’s name. It doesn’t use instanceof, because a bug passes instanceof Error too, so that check can’t pick out the date guard’s plain Error. For the three types checkRun() throws, it reports the run, the push is skipped, and the loop moves on. Anything else is a bug, so throw error; sends it on.
  • That check has a limit. A bug that throws one of those three types would still be skipped as a bad run. Error types of your own, later on the path, tell the two apart.
  • So throw where the problem is found, in checkRun(). Catch where the code can respond, in the loop that can report the run and skip it. A function that can’t respond has no catch, and the error climbs past it.

Final Code

This is check.js, unchanged from the last lesson, which throws for a bad run:

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 Ruth’s checkRun() now throws an error that names the run and the problem. But one bad run still stops the whole import. In this video, you’ll find where to catch that error.

00:11 She moves the loop out of loadRuns(), so the import has its own step, where a bad run could be skipped. A new function, importRuns(), loops over the runs.

00:21 main.js now sends the runs through importRuns() to the report.

00:26 Her new runs are from May 23, and Grace Kim’s has no distance. Run the season log, and nothing prints but a report. It’s Ruth’s own TypeError, with her message: run 22, distance must be a number.

00:42 Even the good runs from Priya and Kwame never printed. Ruth wants the bad run reported and skipped, and the rest printed. So where should the catch go? To find out, follow the error from the moment checkRun() throws it.

00:57 Each call puts a frame on the call stack. The bottom note is the top level of main.js, paused at line 6, where it calls importRuns().

01:06 That function pauses at line 23 to call checkRun(), whose note lands on top. Now checkRun() throws. JavaScript looks for a try around that line, and there’s none.

01:18 So checkRun() stops there, its note comes off, and the error comes out at its call, line 23. That line isn’t inside a try either.

01:27 So importRuns() stops too, and the error comes out at line 6 of main.js. Nothing catches it there, so the program stops, and Node prints the report.

01:39 The at lines, read from the top, are the function that created this error, then the calls waiting under it. Here it’s created where it’s thrown, so they also trace its climb.

01:52 A natural first fix is to catch the error where it happens. Ruth wraps the guards in checkRun() in a try. Its catch logs the message and returns.

02:02 Run it, and there’s no crash. The message prints, then all three runs, Grace’s with undefined kilometers.

02:11 checkRun() knew that run was bad, but its caller never found out. To importRuns(), the call returned as it does for a good run, so the loop kept it.

02:21 Watch out for a function that logs a problem and carries on, with a catch like this one, or with a log where a throw should be.

02:28 The code that could respond never hears about it.

02:33 So Ruth takes the try back out of checkRun(). The code that can skip a run is the loop in importRuns(). There, a small try holds the check and the push.

02:44 Its catch checks the error’s name. It doesn’t use instanceof, which can’t pick out the date guard’s plain Error. For the three types checkRun() throws, it logs Skipped and the message.

02:55 Anything else is a bug, so it’s thrown again. Your turn: pause the video and predict what the season log prints now.

03:09 First comes the skipped line for run 22, because the import runs before anything prints. Then Priya’s and Kwame’s runs print, and their minutes. Grace’s is left out.

03:21 Watch the stack this time. When checkRun() throws, its note comes off, and the error comes out at its call, line 24. That line is inside the try.

03:31 So the catch stops the error, the push is skipped, and the loop moves on to Kwame’s run. This check has one limit. A bug that throws one of those three types would still be skipped as a bad run.

03:44 Error types of your own, later on the path, tell them apart.

03:49 So throw where the problem is found, in checkRun(). Catch where the code can respond, in the loop that can report the run and skip it. A function that can’t respond has no catch, and the error climbs past it.

04:03 In the next lesson, Decide When to Throw and Wrap, you’ll see when a special return value reports a problem better than a throw.

Become a Member to join the conversation.