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
Tests as executable checks of program behavior
Video
This overview introduces exception handling with try, except, else, and finally.
Python Tutorial: Using Try/Except Blocks for Error Handling by Corey Schafer
Learning Goals
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
Write
doctest,unittest, andpytesttests to verify behavior