Skip to content
Computing · Year 9

Active learning ideas

Error Handling: Try-Except Blocks

Active learning immerses students in authentic debugging tasks where errors are expected and handled, making abstract concepts concrete. For error handling with try-except blocks, students need to experience crashes firsthand to understand why graceful recovery matters in real programs.

National Curriculum Attainment TargetsKS3: Computing - Programming and DevelopmentKS3: Computing - Debugging
20–45 minPairs → Whole Class4 activities

Activity 01

Problem-Based Learning30 min · Pairs

Pair Programming: Safe Calculator

Pairs write a calculator program that takes two numbers from user input. Add a try-except block to handle division by zero with a polite error message and prompt retry. Test with invalid inputs like letters to refine except clauses.

Explain the importance of error handling in creating robust and user-friendly programs.

Facilitation TipDuring Safe Calculator, circulate and ask each pair to predict what error would occur with invalid inputs before running the code.

What to look forProvide students with a short Python code snippet that includes a potential ZeroDivisionError. Ask them to write the try-except block needed to handle this error and explain in one sentence what their code does if the error occurs.

AnalyzeEvaluateCreateDecision-MakingSelf-ManagementRelationship Skills
Generate Complete Lesson

Activity 02

Problem-Based Learning45 min · Small Groups

Small Group Challenge: Input Validator

Groups create a program asking for age input and categorizing users. Use try-except for ValueError on non-numbers, offering re-entry options. Extend to handle multiple input types, sharing code snippets for peer feedback.

Construct a Python program that safely handles a 'division by zero' error.

Facilitation TipIn Input Validator, assign each group a different data type to validate so they see varied exception types in action.

What to look forPresent students with a scenario: A program asks users for their age, but they might type text instead of a number. Ask them to identify the type of error this would cause (ValueError) and write the Python code for a try-except block to handle it.

AnalyzeEvaluateCreateDecision-MakingSelf-ManagementRelationship Skills
Generate Complete Lesson

Activity 03

Problem-Based Learning25 min · Whole Class

Whole Class Hunt: Error Scenarios

Display buggy code on screen. Class brainstorms common runtime errors, then votes on best try-except solutions. Volunteers code fixes live, with everyone predicting outcomes before running.

Analyze how different types of errors might require different handling strategies.

Facilitation TipFor the Error Scenarios hunt, provide printed code snippets so students annotate where try-except blocks should be added.

What to look forStudents write a small Python program that takes two numbers as input and divides them. They then swap programs with a partner. Each student reviews their partner's code to ensure a try-except block is implemented to catch ZeroDivisionError and provides one piece of feedback on clarity or effectiveness.

AnalyzeEvaluateCreateDecision-MakingSelf-ManagementRelationship Skills
Generate Complete Lesson

Activity 04

Problem-Based Learning20 min · Individual

Individual Extension: Multi-Exception Handler

Students modify a file reader program to catch FileNotFoundError, PermissionError, and generic exceptions. Log errors to a list for later analysis, then demo one unique case.

Explain the importance of error handling in creating robust and user-friendly programs.

Facilitation TipIn Multi-Exception Handler, challenge students to time their code with and without error handling to measure impact.

What to look forProvide students with a short Python code snippet that includes a potential ZeroDivisionError. Ask them to write the try-except block needed to handle this error and explain in one sentence what their code does if the error occurs.

AnalyzeEvaluateCreateDecision-MakingSelf-ManagementRelationship Skills
Generate Complete Lesson

A few notes on teaching this unit

Teach error handling by starting with broken code students can run and crash, then refactor it together. Emphasize specificity in exception types rather than broad catches, and use timing comparisons to address performance concerns. Model debugging aloud to show how to isolate errors systematically.

Students will confidently write try-except blocks that catch specific exceptions and provide meaningful user feedback. They will explain why error handling improves program reliability and debug collaboratively to strengthen their code.


Watch Out for These Misconceptions

  • During Pair Programming: Safe Calculator, watch for students who treat any error as a generic exception and catch everything with a single except block.

    Prompt pairs to test with invalid inputs like letters or empty strings, then guide them to identify the exact exception types raised, such as ValueError or ZeroDivisionError, before refining their except clauses.

  • During Small Group Challenge: Input Validator, watch for students who assume one except block can handle all input errors.

    Have groups swap their validator programs and deliberately input wrong data types, then revise their code to include separate except blocks for ValueError, TypeError, and others as needed.

  • During Whole Class Hunt: Error Scenarios, watch for students who think error handling makes programs run slower in all cases.

    During the timing comparison, ask students to run the same program with and without try-except blocks using large datasets, then discuss why the overhead is negligible for graceful recovery versus crashes.


Methods used in this brief