5. Exceptions & Testing#
Errors are inevitable in any program. This chapter teaches you to recover from runtime failures and find logic errors before they reach users.
Exception handling for runtime failures
Tracebacks as debugging evidence
Custom exceptions for clearer error reporting
Debugging techniques and logging to find and record problems
Tests as executable checks of program behavior
Chapter Video
This overview introduces exception handling with try, except, else, and finally.
Python Tutorial: Using Try/Except Blocks for Error Handling (10:34) by Corey Schafer
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Distinguish between syntax errors, runtime errors, and semantic errors
Read and interpret Python tracebacks to locate the source of a bug
Handle exceptions gracefully with
try,except,else, andfinallyRaise exceptions and define custom exception classes
Track down bugs with print-statement debugging, assertions, and bisection, and record diagnostics with
loggingWrite tests with
pytest,unittest, anddoctest, including tests that expect an exceptionIsolate code from dependencies with mocks, test many inputs with parametrization, and read a coverage report
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Exception |
An object that represents an error or unusual condition during program execution. |
2 |
Exception handling |
Code that responds to exceptions so a program can recover or fail clearly. |
3 |
|
The block of code where Python watches for exceptions. |
4 |
|
The block that runs when a matching exception occurs. |
5 |
|
The block that runs after a |
6 |
|
The block that runs whether or not an exception occurs. |
7 |
Traceback |
A report showing the call path and location of an exception. |
8 |
Debugging |
The process of finding and fixing problems in a program. |
9 |
Runtime error |
An error that occurs while a program is running. |
10 |
Semantic error |
A logic error where code runs but produces the wrong result. |
11 |
Raise |
To deliberately signal an exception with the |
12 |
Custom exception |
An exception class defined for a program-specific error case. |
13 |
Logging |
Recording events, warnings, and errors from a running program. |
14 |
Log level |
A severity category for a log message, such as DEBUG, INFO, WARNING, ERROR, or CRITICAL. |
15 |
Unit test |
A test that checks a small part of a program in isolation. |
16 |
Assertion |
A check, written with |
17 |
Doctest |
A test written in a docstring using interactive Python examples. |
18 |
Fixture |
Setup data or resources used by tests. |
19 |
Mock |
A replacement object used to isolate code from an external dependency. |
20 |
Test coverage |
A measure of which lines or branches of code were exercised by tests. |
21 |
Syntax error |
An error in code that violates Python’s grammar, reported before any of the program runs. |
22 |
Bisection |
A debugging technique that checks the program’s state at a midpoint to find which half contains the bug, then repeats on that half. |
23 |
Parametrized test |
A single test function that runs once for each set of inputs, written with |
24 |
Dependency injection |
A design in which a function receives the objects it depends on as arguments, so a test can pass in a stand-in. |
25 |
Fake |
A simple stand-in object that returns canned data in place of a real dependency during a test. |
26 |
Integration test |
A test that checks several units of a program working together. |
27 |
Test-driven development |
A workflow in which you write the tests first, then write the code that makes them pass. |
28 |
Test discovery |
The way |