← Python

Error Handling – try/except basics

A try/except block runs code and catches errors so the program does not crash

Understanding Error Handling – try/except basics in Python

What it does: Naming the exception type catches only that specific kind of error. try/except intercepts errors and lets you handle them gracefully. Put risky code inside try — if it fails, except handles it. If try succeeds, except is completely skipped. int('abc') causes a ValueError — except catches it and prints 'Oops'. 5 + 3 works fine — no error, so except is skipped. 'hello' can't be converted to an integer — that's a ValueError. except always needs a matching try block before it.

Common mistakes: Missing colon after try. Missing colon after except.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 21–30) and word search (Modules 21–40).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What does try/except do?
Catches errors so the program doesn't crashMakes code faster
Matches
❌ try: x = int('abc') exept: print('oops')
↓
Error: Typo — 'except' not 'exept'
Applying
Meanings
Term
try
Meaning
Block containing code that might cause an error.
Practice
try:
Testing
Gaps
:
Check Answer
Spots
1try
2 num = int(input('Number: '))

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Void Whale' badge — Definition Drifter, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What does try/except do?
A. Catches errors so the program doesn't crash ✓
B. Makes code faster
try/except intercepts errors and lets you handle them gracefully
2. What goes inside the try block?
A. Error messages
B. Code that might cause an error ✓
Put risky code inside try — if it fails, except handles it
3. When does the except block run?
A. Always
B. Only when an error occurs in try ✓
If try succeeds, except is completely skipped
4. try:\n int('abc')\nexcept:\n print('Oops')\nWhat prints?
A. Oops ✓
B. abc
int('abc') causes a ValueError — except catches it and prints 'Oops'
5. try:\n print(5 + 3)\nexcept:\n print('Error')\nWhat prints?
A. 8 ✓
B. Error
5 + 3 works fine — no error, so except is skipped

Match the Pairs

try: x = int('abc') exept: print('oops') → Error: Typo — 'except' not 'exept'
except: print('error') try: risky() → Error: try must come before except
try: x = int(val) except ValueError: print('bad') → Valid: Catches ValueError only
try: risky() except: print('handled') → Ok: Catches any error
try: print(int('hi')) except: print('error') → Output: error
try: print(int('5')) except: print('error') → Output: 5

Key Term

try
Block containing code that might cause an error.~~except

Fill in the Blanks

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

Spot the Error

Question 1
A try
B num = int(input('Number: '))
C except:
D print('Invalid')
Line A — Missing colon after try
Question 2
A try:
B num = int(input('Number: '))
C except
D print('Invalid')
Line C — Missing colon after except
Question 3
A try:
B num = int(input('Number: '))
C except:
D print('Invalid')
Line B — Code inside try must be indented
Question 4
A except:
B print('Invalid')
C print('Try again')
D print('Done')
Line A — except cannot stand alone: it must follow a try block
Question 5
A Try:
B num = int('abc')
C Except:
D print('Error')
Line A — try and except must be lowercase

Student with a class code? Join your class →