Error Handling – try/catch, online

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

Error Handling – try/catch in one minute

What students need to know

Errors in try are caught instead of stopping the whole program. The catch parameter stores details about what went wrong.

The catch block runs instead of the program crashing. If no error occurs, the catch block is simply skipped.

Mistakes students make

  • 'try' must be lowercase - a capital T causes an error.
  • 'console' is misspelled as 'consol'.
  • 'catch' must be lowercase - a capital C causes an error.
  • This line is missing a semicolon, but that's not required in JS.
PREVIOUS · MODULE 43Functions – Scope
YOU ARE HERE · MODULE 44Error Handling – try/catch
NEXT · MODULE 45throw Statement

Discover other journeys with this module

What students will learn in this module

Every question in Module 44

Quick questions

  1. Does try/catch let a program keep running after an error?AYesBNo
  2. Does the catch block run only when an error occurs in try?ANoBYes
  3. Must 'try' be written in lowercase?AYesBNo
  4. Must 'catch' be written in lowercase?AYesBNo
  5. Does catch's parameter hold information about the error?ANoBYes

Key terms

Students type the meaning of:

  • try...catch

Match the pairs

try { risky(); } catch (e) { ... }Match it to what it does
catch (e) { console.log(e); }Match it to what it does
Prevents a program from crashing when an error occursMatch it to what it does
Holds information about the error inside catchMatch it to what it does
Try { console.log(x); } catch (e) {}Match it to what it does
try { console.log(x); } Catch (e) {}Match it to what it does

Fill in the blanks

  1. ___ {
    console.log(risky);
    } catch (e) {
    }

    Hint: Type the keyword that starts the block of code to attempt.

  2. try {
    doSomething();
    } _____ (e) {
    console.log(e);
    }

    Hint: Type the keyword that runs its block only if an error occurs.

  3. try {
    parseData();
    } catch (___) {
    }

    Hint: Type the parameter name that holds the error.

  4. ___ {
    loadFile();
    } catch (problem) {
    }

    Hint: Type the keyword that starts the block of code to attempt.

  5. try {
    connect();
    } _____ (problem) {
    console.log(problem);
    }

    Hint: Type the keyword that runs its block only if an error occurs.

Spot the error

Question 1

ATry {Bconsole.log(a);C} catch (e) {D}

Question 2

Atry {Bconsol.log(b);C} catch (e) {D}

Question 3

Atry {Bconsole.log(c);C} Catch (e) {D}

Question 4

Atry {Bconsole.log(d);C} catch (e) {Dconsole.log(e)E}

Question 5

Atry {Bconsole.log(g);C} catch (e) {Dconsole.log(e);E}}