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.
What students will learn in this module
Every question in Module 44
Quick questions
- Does try/catch let a program keep running after an error?AYesBNo
- Does the catch block run only when an error occurs in try?ANoBYes
- Must 'try' be written in lowercase?AYesBNo
- Must 'catch' be written in lowercase?AYesBNo
- 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 doescatch (e) { console.log(e); }Match it to what it doesPrevents a program from crashing when an error occursMatch it to what it doesHolds information about the error inside catchMatch it to what it doesTry { console.log(x); } catch (e) {}Match it to what it doestry { console.log(x); } Catch (e) {}Match it to what it doesFill in the blanks
___ { console.log(risky); } catch (e) { }Hint: Type the keyword that starts the block of code to attempt.
try { doSomething(); } _____ (e) { console.log(e); }Hint: Type the keyword that runs its block only if an error occurs.
try { parseData(); } catch (___) { }Hint: Type the parameter name that holds the error.
___ { loadFile(); } catch (problem) { }Hint: Type the keyword that starts the block of code to attempt.
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}}
