Skip to content

Error Handling: Try-Except BlocksActivities & Teaching Strategies

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.

Year 9Computing4 activities20 min45 min

Learning Objectives

  1. 1Construct Python programs that utilize try-except blocks to handle specific runtime errors like ZeroDivisionError and ValueError.
  2. 2Analyze the execution flow of a program when an exception occurs within a try block.
  3. 3Compare the outcomes of running code with and without appropriate exception handling for predictable errors.
  4. 4Explain the role of except blocks in providing alternative execution paths when errors are caught.
  5. 5Design a simple Python application that gracefully manages user input errors.

Want a complete lesson plan with these objectives? Generate a Mission

30 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.

Prepare & details

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

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

Setup: Groups at tables with access to research materials

Materials: Problem scenario document, KWL chart or inquiry framework, Resource library, Solution presentation template

AnalyzeEvaluateCreateDecision-MakingSelf-ManagementRelationship Skills
45 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.

Prepare & details

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

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

Setup: Groups at tables with access to research materials

Materials: Problem scenario document, KWL chart or inquiry framework, Resource library, Solution presentation template

AnalyzeEvaluateCreateDecision-MakingSelf-ManagementRelationship Skills
25 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.

Prepare & details

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

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

Setup: Groups at tables with access to research materials

Materials: Problem scenario document, KWL chart or inquiry framework, Resource library, Solution presentation template

AnalyzeEvaluateCreateDecision-MakingSelf-ManagementRelationship Skills
20 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.

Prepare & details

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

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

Setup: Groups at tables with access to research materials

Materials: Problem scenario document, KWL chart or inquiry framework, Resource library, Solution presentation template

AnalyzeEvaluateCreateDecision-MakingSelf-ManagementRelationship Skills

Teaching This Topic

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.

What to Expect

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.

These activities are a starting point. A full mission is the experience.

  • Complete facilitation script with teacher dialogue
  • Printable student materials, ready for class
  • Differentiation strategies for every learner
Generate a Mission

Watch Out for These Misconceptions

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

What to Teach Instead

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.

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

What to Teach Instead

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.

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

What to Teach Instead

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.

Assessment Ideas

Exit Ticket

After Pair Programming: Safe Calculator, give students a short code snippet with a potential ZeroDivisionError. Ask them to write the correct try-except block and explain in one sentence what happens if the error occurs.

Quick Check

During Small Group Challenge: Input Validator, present a scenario where a user enters text for their age. Ask students to identify the exception type (ValueError) and write the try-except block to handle it.

Peer Assessment

After Whole Class Hunt: Error Scenarios, have students swap programs and review each other’s try-except blocks for catching ZeroDivisionError. Each student provides one piece of feedback on clarity or effectiveness before returning the code for revision.

Extensions & Scaffolding

  • Challenge students to extend their Multi-Exception Handler to log errors to a file for later review.
  • Scaffolding: Provide partially completed try-except templates with placeholders for students to fill in specific exception types.
  • Deeper exploration: Ask students to research and implement custom exceptions for a program that processes student grades.

Key Vocabulary

ExceptionAn event that occurs during program execution that disrupts the normal flow of instructions. Common examples include trying to divide by zero or converting text to a number incorrectly.
Try BlockA section of code where potential exceptions might occur. The program monitors this block for errors.
Except BlockA section of code that runs only if a specific exception is detected within the preceding try block. It defines how to handle the error.
Runtime ErrorAn error that occurs while the program is running, as opposed to a syntax error found before execution. Exception handling is designed to manage these.
ZeroDivisionErrorA specific type of runtime error that occurs when a program attempts to divide a number by zero.
ValueErrorA specific type of runtime error that occurs when a function receives an argument of the correct type but an inappropriate value, such as trying to convert the string 'hello' into an integer.

Ready to teach Error Handling: Try-Except Blocks?

Generate a full mission with everything you need

Generate a Mission