Error Handling – try/catch

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

Error Handling – try/catch

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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

Match the Pairs

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

Left columnRight column
1try { risky(); } catch (e) { ... } Ae
2catch (e) { console.log(e); } BRuns this block only if an error occurred in try
3Prevents a program from crashing when an error occurs Ctry { risky(); } catch (e) {}
4Holds information about the error inside catch DWrong - 'try' must be lowercase
5Try { console.log(x); } catch (e) {} ERuns the try block, and if it errors, runs catch
6try { console.log(x); } Catch (e) {} FWrong - 'catch' must be lowercase

Definitions

try...catch

Fill in the Blanks

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

1.
___ { console.log(risky); } catch (e) { }
2.
try { doSomething(); } _____ (e) { console.log(e); }
3.
try { parseData(); } catch (___) { }
4.
___ { loadFile(); } catch (problem) { }
5.
try { connect(); } _____ (problem) { console.log(problem); }

Spot the Error

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

Question 1
A Try {
B console.log(a);
C } catch (e) {
D }
Question 2
A try {
B consol.log(b);
C } catch (e) {
D }
Question 3
A try {
B console.log(c);
C } Catch (e) {
D }
Question 4
A try {
B console.log(d);
C } catch (e) {
D console.log(e)
E }
Question 5
A try {
B console.log(g);
C } catch (e) {
D console.log(e);
E }}

Answer Key

Error Handling – try/catch
Section A: Quick Questions
Q1
A — Yes
Q2
B — Yes
Q3
A — Yes
Q4
A — Yes
Q5
B — Yes
Section B: Match the Pairs
Q1
try { risky(); } catch (e) { ... } → Runs the try block, and if it errors, runs catch
Q2
catch (e) { console.log(e); } → Runs this block only if an error occurred in try
Q3
Prevents a program from crashing when an error occurs → try { risky(); } catch (e) {}
Q4
Holds information about the error inside catch → e
Q5
Try { console.log(x); } catch (e) {} → Wrong - 'try' must be lowercase
Q6
try { console.log(x); } Catch (e) {} → Wrong - 'catch' must be lowercase
Section C: Definitions
Q1
try...catch — Runs code in a try block, and if an error occurs, runs the catch block instead of crashing.
Section D: Fill in the Blanks
Q1
try { console.log(risky); } catch (e) { }
Q2
try { doSomething(); } catch (e) { console.log(e); }
Q3
try { parseData(); } catch (err) { }
Q4
try { loadFile(); } catch (problem) { }
Q5
try { connect(); } catch (problem) { console.log(problem); }
Section E: Spot the Error
Q1
A — 'try' must be lowercase - a capital T causes an error.
Q2
B — 'console' is misspelled as 'consol'.
Q3
C — 'catch' must be lowercase - a capital C causes an error.
Q4
D — This line is missing a semicolon, but that's not required in JS.
Q5
E — There's an extra closing brace - only one } is needed here.