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

Watch a Throw Jump to catch

In this video, you caught the SyntaxError from Ruth’s hand-edited season file and watched which lines of main.js run, and in what order, when a line inside try throws.

Key Points

  • Ruth typed a seventh run into runs.json by hand and left a comma after its last value. JSON.parse() throws a SyntaxError inside loadRuns(), and because nothing catches it, the whole program stops with a report and no runs print.
  • A try statement wraps the lines that might throw. If any line in the try block throws, the catch block runs instead, and its error parameter holds the error that was thrown.
  • The error is an object. Its .name property names the error’s type, and its .message property is the text after it. Together, they make up the error line in a report.
  • When line 5 throws, the rest of try is skipped and JavaScript jumps straight to catch, so printSeason() never runs. After catch finishes, the program carries on after the whole statement and ends normally.
  • Watch out: once catch has run, JavaScript never goes back into try. Anything that should still happen goes after the statement.
  • A finally block always runs on the way out of the statement, whether try finished or catch ran.
  • Inside a function, a return in try or catch leaves before the line after the statement, but it can’t skip finally. The toy sketch.js below has no catch at all, since a try can be followed by finally alone. Its try returns, yet the line in finally prints first, and only then does the function hand back its text. The line after the statement inside the function never runs.
  • So a throw in try jumps to catch, then finally, which always runs. Once catch and finally finish without a return, the line after the statement runs. It never goes back into try.
  • With the comma removed, try finishes, catch is skipped, and finally and the line after the statement run as before.

Final Code

Ruth’s other files in the season-log folder haven’t changed in this video, apart from Grace Kim’s run at the end of runs.json:

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 has typed in the next Saturday’s run by hand. It’s Grace Kim’s, at the end of runs.json, and she left a comma after its last value. Run the season log, and nothing prints but a report.

00:13 It’s a SyntaxError. Inside loadRuns(), JSON.parse() threw it. After that last comma, JSON expects another property name, and it finds a closing brace instead.

00:26 An error that nothing catches stops the whole program. Ruth would rather see one line about the broken file and have the program carry on. In this video, you’ll catch that error and watch, line by line, which code runs.

00:41 You do that with a try statement. The two lines that might throw, the one that loads the runs and the one that prints them, go inside its try block.

00:51 Right after it comes a catch block. If any line in try throws, catch runs instead, and its error parameter holds the error that was thrown.

01:01 That error is an object. Its name property names the error’s type, and its message property is the text after it. They’re the two halves of the error line you’ve been reading.

01:11 So this catch logs one line with both. After the whole statement, Ruth adds one more line. It’s a stand-in for whatever her program would do next.

01:24 Run it, and follow the lines. Line 5 of main.js calls loadRuns(), and inside it, JSON.parse() throws. The error leaves loadRuns() and comes back out at line 5, the call inside try.

01:39 The rest of try is skipped. JavaScript jumps straight from line 5 to catch, so printSeason() never runs. The catch block logs the error’s name and message.

01:52 Then the program carries on after the whole statement and logs its last line. It ends normally, with no report. Watch out: once catch has run, JavaScript never goes back into try to finish it.

02:06 Anything that should still happen goes after the statement.

02:11 A try statement can also end with a finally block. Its lines run on the way out of the statement, whether try finished or catch ran.

02:19 Ruth uses it to log which file she checked. Run it again. Out comes the error line from catch, then the check line from finally, then the last line.

02:31 The check line could go after the statement instead, but in a function, a return leaves before that line. A return can’t skip finally.

02:40 In this toy function, with no catch at all, try returns, yet the check line prints first. Only then does the function hand back its text.

02:50 Ruth deletes the comma after Grace’s minutes, so the file is valid JSON again. Your turn: pause the video and predict what the season log prints now, in order.

03:06 All seven runs print, because try finishes. The catch block is skipped, since nothing threw. Then comes the check line from finally, and the last line.

03:18 So when a line in try throws, the rest of try is skipped and catch runs. Then finally always runs, even when a return is waiting.

03:27 Once catch and finally finish without a return, the program carries on after the statement. It never goes back into try to finish it.

03:36 Right now, Ruth’s catch catches anything that goes wrong in those two lines, not only a broken file. In the next lesson, Catch Only What You Can Handle, you’ll decide what a catch should catch.

Become a Member to join the conversation.