← Python

Error Handling – try/except basics

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

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 journey and assign it to your class.

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. Only when an error occurs in try ✓
B. Always
If try succeeds, except is completely skipped
4. try:\n int('abc')\nexcept:\n print('Oops')\nWhat prints?
A. abc
B. Oops ✓
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
For Teachers

Create a free class, add this module to a journey, and track who's stuck.

Create a free teacher account →

No credit card. No setup fee. Make a class and invite students in under three minutes.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?