liveBook · Manning

Chapter 14: Exceptions

What you will learn in chapter 14 of The Quick Python Book, Fourth Edition

The chapter introduces exceptions as Python’s way to handle unusual circumstances during program execution.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces exceptions as Python’s way to handle unusual circumstances during program execution.

Key terms: exceptions, errors

14.1 Introduction to exceptions

14.1.1 General philosophy of errors and exception handling

The section uses a disk-full word-processor example to show why exceptions are preferable to ignoring errors or passing failure status through every function call.

Key terms: exception mechanism, propagates, save_to_file, success/failure status, status value, plumbing code, disk-space problem, mission-critical programs

14.1.2 A more formal definition of exceptions

This section defines raising an exception, catching it, and the code that handles it.

Key terms: raising, throwing, catching, exception handler, exception-handling code

14.1.3 Handling different types of exceptions

The section explains why Python uses different exception types and how handlers can respond selectively to them.

Key terms: types of exceptions, raised, exception handler, catch

14.2 Exceptions in Python

Introduces how Python raises and handles exception objects, and why Python often handles errors after they occur rather than checking for every possibility beforehand.

Key terms: exception, exception object, raise statement, call chain, handler, easier to ask forgiveness than permission, EAFP, look before you leap, LBYL

14.2.1 Types of Python exceptions

This section explains how Python exception types form an inheritance hierarchy and why user-defined exceptions should normally inherit from Exception rather than BaseException.

Key terms: exception types, exception hierarchy, inheritance, IndexError, LookupError, Exception, BaseException, KeyboardInterrupt, user-defined exceptions, Ctrl-C

14.2.2 Raising exceptions

This section explains how Python and user code raise exceptions, how raised exceptions travel to a catcher, and what their arguments can mean.

Key terms: raise statement, IndexError, exception catcher, stack, exception(args), string argument, exception type, class definition

14.2.3 Catching and handling exceptions

This section explains how a try statement catches exceptions, what happens when no handler matches, and when else and finally blocks run.

Key terms: try, except, exception handler, ZeroDivisionError, call chain, as, exception type, tuple of exceptions, else, finally

14.2.4 Defining new exceptions

This section shows how to define a custom exception, pass information with it, and choose an exception hierarchy for a program or library.

Key terms: Exception class, exception type, raise, catch, handle, argument, traceback, handler, args, tuple

14.2.5 Exception groups

This section explains why Python groups multiple exceptions and how except* handles the exceptions within a group.

Key terms: exception groups, concurrent programming, ExceptionGroup, except*, except, IOError, FileNotFoundError

14.2.6 Debugging programs with the assert statement

This section explains how assert raises an AssertionError during debugging and how optimization disables assertions without runtime cost.

Key terms: assert statement, AssertionError, __debug__, optional argument, -O, -OO, PYTHONOPTIMIZE, assert statements

14.2.7 The exception inheritance hierarchy

This section explains how exception inheritance and clause order determine which except clause catches an error.

Key terms: IndexError, LookupError, subclass, except clause, inheritance

14.2.8 Example: A disk-writing program in Python

The disk-writing example shows how exception handling can stay outside the main writing sequence and how an I/O error can be distinguished from a disk-full condition.

Key terms: save_to_file, error-handling code, subsidiary disk-writing functions, IOError, disk-full problem, except body, call chain

14.2.9 Example: Exceptions in normal evaluation

This section shows how exceptions can support normal spreadsheet calculations by converting cell contents and returning None when a formula cannot be evaluated.

Key terms: float, ValueError, None, safe_apply, spreadsheet formulas, standard Python arithmetic operators

14.2.10 Where to use exceptions

This subsection identifies when exceptions are useful: handling errors, including errors addressed late in development, and abandoning processing on an untenable computational branch.

Key terms: exceptions, error handling, after-the-fact error-handling code, computational branch, branch-and-bound algorithms, parsing algorithms

14.3 Context managers using the with keyword

This section explains how context managers use with blocks to manage resources, especially closing files after use.

Key terms: context managers, with keyword, file objects, routine cleanup, with statements, parentheses

14.4 Adding exceptions

This section asks the reader to identify plausible errors in a word-frequency module and refactor its functions to handle them appropriately.

14.4.1 Solving the problem with AI-generated code

This subsection suggests using the previous version of a program as the basis for an AI prompt to add exceptions.

Key terms: prompt, previous version of the code, exceptions

14.4.2 Solutions and discussion

14.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 13: Reading and writing files Chapter 15: Classes and object-oriented programming →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.