Module 8
Error Handling and Robust Code
// Lessons
5
// Quiz questions
75
// Reading time
130 min
// Exam length
15 Q
// Coding exercises
9
- 1Syntax Errors vs Runtime ExceptionsDistinguish compile-time parser errors from runtime crashes and interpret traceback callstacks. 25 min 15 questions
- 2Exception Handling PatternsCatch specific error hierarchies, isolate success paths with else, and ensure resource cleanup with finally. 25 min 15 questions
- 3Custom Exception HierarchiesSubclass Exception to architect domain-specific error trees and communicate actionable failure details. 25 min 15 questions
- 4Defensive Assertions & InvariantsVerify internal contract assumptions, check pre-conditions, and avoid dangerous assertion abuse. 25 min 15 questions
- 5Structural Pattern MatchingBranch cleanly on complex data shapes using match-case statements, sequence patterns, and guard clauses. 30 min 15 questions
Coding practice: write it yourself
Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (4 exercises)
Coding practice needs an account. Sign in or create a free ID to run these exercises.
Lab 1: Divide without crashing
Divide 900 by people = 0 inside try/except so that instead of a crash it prints People must be more than zero.
Coding Practice is Locked
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.
Lab 2: Parse a number safely
The text "abc" should become a number. Convert it inside try/except ValueError; on failure print not a number.
Coding Practice is Locked
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.
Lab 3: A Dog class
Create a class Dog whose __init__ takes name, with a method bark() returning "{name} says Woof". Print Dog("Rex").bark().
Coding Practice is Locked
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.
Lab 4: Inherit and override
Animal.speak() returns "...". Make Cat(Animal) override it to return "Meow", then print Cat().speak().
Coding Practice is Locked
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.
References & Further Reading
Related entries from PyLabs's own reference library.
- open() (Open a file so you can read from it or write to it.)
- isinstance() (Check whether a value is an instance of a type.)
- iter() (Return an iterator for a collection or other iterable.)