throw Statement, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
throw Statement in one minute
What students need to know
throw is exactly the keyword that does this. A thrown error inside try can be handled by catch.
throw raises an error that can be handled elsewhere. Execution stops immediately at the point throw is called.
Mistakes students make
- 'throw' must be lowercase - a capital T causes an error.
- 'Error' must start with a capital E, not lowercase.
- This line is actually correct - throw can raise a plain string too.
- 'throw' is misspelled as 'trow'.
PREVIOUS · MODULE 44Error Handling – try/catch
YOU ARE HERE · MODULE 45throw Statement
NEXT · MODULE 46JSON – stringify and parse
What students will learn in this module
Every question in Module 45
Quick questions
- Does throw create a custom error?AYesBNo
- Does throw stop the current code from running further?ANoBYes
- Can a thrown error be caught by a catch block?AYesBNo
- Must 'throw' be written in lowercase?ANoBYes
- Must 'Error' start with a capital E when using new Error()?AYesBNo
Key terms
Students type the meaning of:
throw
Match the pairs
throw new Error("Something broke");Match it to what it doesthrow new Error("Bad age");Match it to what it doesCreates and raises a custom errorMatch it to what it doesCan be caught by a matching catch blockMatch it to what it doesThrow new Error("Bad");Match it to what it doesthrow new error("Bad");Match it to what it doesFill in the blanks
_____ new Error("Something broke");Hint: Type the keyword that raises a custom error.
throw new _____("Invalid input");Hint: Type the built-in type used to create an error.
_____ new Error("No data found");Hint: Type the keyword that raises a custom error.
if (age < 0) { _____ new Error("Bad age"); }Hint: Type the keyword that raises a custom error.
throw new _____("Out of range");Hint: Type the built-in type used to create an error.
Spot the error
Question 1
AThrow new Error("Bad");Bconsole.log("after");Clet x = 1;
Question 2
Athrow new error("Bad");Bconsole.log("after");Clet x = 1;
Question 3
Alet a = 1;Bthrow "Bad";Cconsole.log(a);
Question 4
Alet b = 2;Btrow new Error("Bad");Cconsole.log(b);
Question 5
Atry {Bthrow new Error("Oops");C} catch (e) {Dconsole.log(e.message)E}
