Error Handling – try/except basics

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

Error Handling – try/except basics

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does try/except do?
A Catches errors so the program doesn't crash
B Makes code faster
2. What goes inside the try block?
A Error messages
B Code that might cause an error
3. When does the except block run?
A Always
B Only when an error occurs in try
4. try:\n int('abc')\nexcept:\n print('Oops')\nWhat prints?
A Oops
B abc
5. try:\n print(5 + 3)\nexcept:\n print('Error')\nWhat prints?
A 8
B Error

Match the Pairs

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

Left columnRight column
1try: x = int('abc') exept: print('oops') AOutput: error
2except: print('error') try: risky() BValid: Catches ValueError only
3try: x = int(val) except ValueError: print('bad') COutput: 5
4try: risky() except: print('handled') DError: Typo — 'except' not 'exept'
5try: print(int('hi')) except: print('error') EError: try must come before except
6try: print(int('5')) except: print('error') FOk: Catches any error

Definitions

try

Fill in the Blanks

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

1.
___: num = int(input('Number: ')) except: print('Invalid')
2.
try: num = int(input('Number: ')) ______: print('Invalid')
3.
try: num = int(input('Number: ')) except __________: print('Not a number')
4.
try: result = 10 / _ except: print('Error')
5.
try: x = ___(input('Enter num: ')) except: print('Bad input')

Spot the Error

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

Question 1
A try
B num = int(input('Number: '))
C except:
D print('Invalid')
Question 2
A try:
B num = int(input('Number: '))
C except
D print('Invalid')
Question 3
A try:
B num = int(input('Number: '))
C except:
D print('Invalid')
Question 4
A except:
B print('Invalid')
C print('Try again')
D print('Done')
Question 5
A Try:
B num = int('abc')
C Except:
D print('Error')

Answer Key

Error Handling – try/except basics
Section A: Quick Questions
Q1
A — Catches errors so the program doesn't crash
Q2
B — Code that might cause an error
Q3
B — Only when an error occurs in try
Q4
A — Oops
Q5
A — 8
Section B: Match the Pairs
Q1
try: x = int('abc') exept: print('oops') → Error: Typo — 'except' not 'exept'
Q2
except: print('error') try: risky() → Error: try must come before except
Q3
try: x = int(val) except ValueError: print('bad') → Valid: Catches ValueError only
Q4
try: risky() except: print('handled') → Ok: Catches any error
Q5
try: print(int('hi')) except: print('error') → Output: error
Q6
try: print(int('5')) except: print('error') → Output: 5
Section C: Definitions
Q1
try — Block containing code that might cause an error.~~except
Section D: Fill in the Blanks
Q1
try: num = int(input('Number: ')) except: print('Invalid')
Q2
try: num = int(input('Number: ')) except: print('Invalid')
Q3
try: num = int(input('Number: ')) except ValueError: print('Not a number')
Q4
try: result = 10 / 0 except: print('Error')
Q5
try: x = int(input('Enter num: ')) except: print('Bad input')
Section E: Spot the Error
Q1
A — Missing colon after try
Q2
C — Missing colon after except
Q3
B — Code inside try must be indented
Q4
A — except cannot stand alone: it must follow a try block
Q5
A — try and except must be lowercase