Chapter 8 Further Reading and Resources

Official Python Documentation

Core Exception Handling

Python Tutorial: Errors and Exceptions https://docs.python.org/3/tutorial/errors.html

The official tutorial chapter on exceptions. Covers the full try/except/else/finally syntax with clear examples. Particularly useful for the sections on exception chaining (raise X from Y) and defining custom exception classes. Start here if you want the canonical reference.

Python Built-in Exceptions https://docs.python.org/3/library/exceptions.html

The complete hierarchy of all built-in exception types with descriptions of when each is raised. Essential reading when you are trying to catch the right exception for a specific situation. The hierarchy diagram at the top of this page shows how all exceptions relate to each other.

logging — Logging facility for Python https://docs.python.org/3/library/logging.html

The full documentation for the logging module. The module is more powerful than most people realize — it supports log rotation, multiple output destinations, structured logging, and custom filters. The "Basic Tutorial" and "Advanced Tutorial" sections in the howto guides are more accessible than the full API reference.

Logging HOWTO https://docs.python.org/3/howto/logging.html

More approachable than the full API docs. Covers the recommended patterns for configuring logging in applications, the difference between library logging and application logging, and how to write logging configurations that work across multiple modules.

Logging Cookbook https://docs.python.org/3/howto/logging-cookbook.html

A collection of practical logging recipes. Useful once you have the basics and want to solve specific problems: logging to multiple files, adding custom fields to log records, logging from multiple threads.


Python Enhancement Proposals (PEPs)

PEP 3151 — Reworking the OS and IO exception hierarchy https://peps.python.org/pep-3151/

Explains why IOError, OSError, EnvironmentError, and related exceptions were unified in Python 3. If you have ever been confused about when to catch IOError vs. OSError, this explains the decision. Relevant when working with files, network connections, and system resources.

PEP 3134 — Exception Chaining and Embedded Tracebacks https://peps.python.org/pep-3134/

The motivation and design behind exception chaining (raise X from Y). Understanding this PEP helps you write exception hierarchies that preserve diagnostic context — when wrapping a lower-level exception in a higher-level one, you want to keep both tracebacks.


Books

"Fluent Python" by Luciano Ramalho (O'Reilly, 2nd edition, 2022) Chapters on context managers and generators are especially relevant to Chapter 8's discussion of finally and resource cleanup. Ramalho's treatment of the with statement and the context manager protocol is among the best available. The book assumes more Python knowledge than this textbook, but the error handling sections are worth reading once you have finished this chapter.

"Clean Code" by Robert C. Martin (Prentice Hall, 2008) Although the examples are in Java, the principles in Chapter 7 ("Error Handling") are directly applicable to Python. Martin makes the argument that error handling should not obscure business logic — exceptions should be thrown at the right abstraction level and caught close to where the problem can actually be handled. The concept of "not returning null" (use exceptions or Optional patterns instead) is highly relevant to the validate_* functions in this chapter.

"Effective Python: 90 Specific Ways to Write Better Python" by Brett Slatkin (Addison-Wesley, 3rd edition, 2024) Items 65 ("Take Advantage of Each Block in try/except/else/finally"), 86 ("Consider warnings to refactor and migrate usage"), and related items provide tightly-focused advice on exception handling patterns. Each item is self-contained and can be read in 10–15 minutes.


Online Tutorials and Articles

"Python Exceptions: An Introduction" — Real Python https://realpython.com/python-exceptions/

A thorough tutorial covering the full exception lifecycle — what they are, how they propagate, how to catch them, and how to create custom ones. Includes worked examples with output. Good for reinforcing the concepts in this chapter with a different explanatory style.

"Logging in Python" — Real Python https://realpython.com/python-logging/

A comprehensive guide to the logging module that goes well beyond basicConfig(). Covers handlers, formatters, log levels, the logger hierarchy, and best practices for structuring logging in real applications. Essential reading before you add logging to a production system.

"Python Logging: A Stroll Through the Source Code" — Real Python https://realpython.com/python-logging-source-code/

For those who want to understand why the logging module works the way it does. Understanding the internals helps when you need to do something non-standard (like adding custom fields to every log record automatically).

"Python Best Practices for File Operations" — Real Python https://realpython.com/working-with-files-in-python/

Covers the intersection of error handling and file operations — the area where Priya and Maya encountered their problems. Explains context managers, path manipulation with pathlib, and the right exceptions to catch for each type of file operation.


Design Patterns and Philosophy

"A Guide to Python's Exception Handling" — Python.land https://python.land/deep-dives/python-try-except

A deep dive into exception design philosophy — when to use exceptions vs. return codes vs. sentinel values. Addresses the controversial question of whether exceptions should be used for control flow and offers nuanced guidance.

"Stop Using Exceptions Like This in Python" — ArjanCodes (YouTube) https://www.youtube.com/c/ArjanCodes

Arjan Egges produces consistently high-quality Python design content. Several of his videos address exception handling patterns directly, including when to use custom exceptions, how to design error hierarchies, and how to write exception-safe code. Search his channel for "Python exceptions" or "Python error handling."


Tools

structlog — Structured Logging for Python https://www.structlog.org/

An alternative to the standard logging module that produces JSON-formatted log output. JSON logs can be indexed and searched by log management tools (Splunk, Elasticsearch, AWS CloudWatch). If you are writing scripts that run in a cloud environment or enterprise logging infrastructure, structlog is worth investigating.

loguru — Python logging made (stupidly) simple https://github.com/Delgan/loguru

A third-party logging library that simplifies configuration significantly. loguru has a single logger object, no handler/formatter/handler setup required for basic use, and excellent support for tracebacks and structured output. A popular choice for scripts and small applications where the standard logging module's setup overhead is frustrating.

tenacity — Retry library for Python https://tenacity.readthedocs.io/

A library for adding retry logic to functions — directly relevant to Exercise 5.4 in this chapter. tenacity handles exponential backoff, maximum retry counts, retry on specific exception types, and logging of retry attempts. Use it instead of writing retry loops by hand in production code.

pydantic — Data validation using Python type annotations https://docs.pydantic.dev/

A data validation library that defines your data schema using Python dataclasses with type annotations and automatically validates incoming data. Pydantic raises ValidationError (its own, very detailed version) when data does not match the schema. The approach in business_validators.py in this chapter represents a manual implementation of what Pydantic does automatically — once you understand the principles, Pydantic is the production tool.


Business Context: Why Data Quality Is a Business Problem

"Data Quality: The Field Guide" by Thomas Redman (Digital Press, 2001) The seminal business-focused book on data quality. Redman argues that poor data quality is not primarily a technical problem but a business process problem — data is entered by humans following business processes, and the errors reflect flaws in those processes. Understanding this context explains why Maya's invoice spreadsheet arrived with "N/A" in a numeric field and why Priya's CSV had inconsistent formats across regions.

"Bad Data Handbook" edited by Q. Ethan McCallum (O'Reilly, 2012) A collection of real-world stories about bad data and what it cost organizations. Each chapter is written by a data practitioner describing a real failure. Invaluable for developing the "defensive" mindset: if you have not yet encountered bad data that crashed a production system, you will.


Practice Datasets for Error Handling Exercises

The following public datasets frequently contain data quality issues that make them excellent for practicing error handling:

US Business Formation Statistics (US Census Bureau) https://www.census.gov/econ/bfs/

Monthly CSVs of new business applications and formations. Has occasional encoding quirks and formatting changes between file vintages that require robust parsing.

Open Payments (CMS.gov) https://openpaymentsdata.cms.gov/

Medical payments data with millions of rows. Contains null values, inconsistent date formats, and mixed-type columns. Processing it at scale requires exactly the patterns covered in this chapter.

NYC Open Data https://opendata.cityofnewyork.us/

Hundreds of real-world municipal datasets with all the data quality characteristics you would expect from government data collected over decades. Filter by "CSV" format and pick any dataset that interests you — almost all of them have something that requires careful error handling to process correctly.