throw Statement, online

Six activities your students do on their own devices. See who's finished, and their scores.

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'.

Discover other journeys with this module

What students will learn in this module

Every question in Module 45

Quick questions

  1. Does throw create a custom error?AYesBNo
  2. Does throw stop the current code from running further?ANoBYes
  3. Can a thrown error be caught by a catch block?AYesBNo
  4. Must 'throw' be written in lowercase?ANoBYes
  5. 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 does
throw new Error("Bad age");Match it to what it does
Creates and raises a custom errorMatch it to what it does
Can be caught by a matching catch blockMatch it to what it does
Throw new Error("Bad");Match it to what it does
throw new error("Bad");Match it to what it does

Fill in the blanks

  1. _____ new Error("Something broke");

    Hint: Type the keyword that raises a custom error.

  2. throw new _____("Invalid input");

    Hint: Type the built-in type used to create an error.

  3. _____ new Error("No data found");

    Hint: Type the keyword that raises a custom error.

  4. if (age < 0) {
    _____ new Error("Bad age");
    }

    Hint: Type the keyword that raises a custom error.

  5. 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}