throw Statement

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

This throw Statement worksheet is part of the JavaScript Coding without HTML and CSS journey. Print it as a booklet?

throw Statement

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does throw create a custom error?
A Yes
B No
2. Does throw stop the current code from running further?
A No
B Yes
3. Can a thrown error be caught by a catch block?
A Yes
B No
4. Must 'throw' be written in lowercase?
A No
B Yes
5. Must 'Error' start with a capital E when using new Error()?
A Yes
B No

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1throw new Error("Something broke"); Athrow
2throw new Error("Bad age"); BWrong - 'Error' must start with a capital E
3Creates and raises a custom error CRaises an error describing an invalid age
4Can be caught by a matching catch block Dtry { throw new Error("X"); } catch (e) { ... }
5Throw new Error("Bad"); EWrong - 'throw' must be lowercase
6throw new error("Bad"); FRaises a custom error and stops execution at that point

Definitions

throw

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
_____ new Error("Something broke");
2.
throw new _____("Invalid input");
3.
_____ new Error("No data found");
4.
if (age < 0) { _____ new Error("Bad age"); }
5.
throw new _____("Out of range");

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Throw new Error("Bad");
B console.log("after");
C let x = 1;
Question 2
A throw new error("Bad");
B console.log("after");
C let x = 1;
Question 3
A let a = 1;
B throw "Bad";
C console.log(a);
Question 4
A let b = 2;
B trow new Error("Bad");
C console.log(b);
Question 5
A try {
B throw new Error("Oops");
C } catch (e) {
D console.log(e.message)
E }

Answer Key

throw Statement
Section A: Quick Questions
Q1
A — Yes
Q2
B — Yes
Q3
A — Yes
Q4
B — Yes
Q5
A — Yes
Section B: Match the Pairs
Q1
throw new Error("Something broke"); → Raises a custom error and stops execution at that point
Q2
throw new Error("Bad age"); → Raises an error describing an invalid age
Q3
Creates and raises a custom error → throw
Q4
Can be caught by a matching catch block → try { throw new Error("X"); } catch (e) { ... }
Q5
Throw new Error("Bad"); → Wrong - 'throw' must be lowercase
Q6
throw new error("Bad"); → Wrong - 'Error' must start with a capital E
Section C: Definitions
Q1
throw — Creates a custom error and immediately stops the current code from running further.
Section D: Fill in the Blanks
Q1
throw new Error("Something broke");
Q2
throw new Error("Invalid input");
Q3
throw new Error("No data found");
Q4
if (age < 0) { throw new Error("Bad age"); }
Q5
throw new Error("Out of range");
Section E: Spot the Error
Q1
A — 'throw' must be lowercase - a capital T causes an error.
Q2
A — 'Error' must start with a capital E, not lowercase.
Q3
B — This line is actually correct - throw can raise a plain string too.
Q4
B — 'throw' is misspelled as 'trow'.
Q5
D — This line is missing a semicolon, but that's not required in JS.