10.11 Exception Class Hierarchy and Custom Exceptions

In the previous chapter, we introduced exception handling. Every exception is an object of a class in Python’s exception class hierarchy11 or an object of a class that inherits from one of those classes. Exception classes inherit directly or indirectly from base class BaseException and are defined in module exceptions.

Python defines four primary BaseException subclasses—SystemExit, KeyboardInterrupt, GeneratorExit and Exception:

Catching Base-Class Exceptions

One of the benefits of the exception class hierarchy is that an except handler can catch exceptions of a particular type or can use a base-class type to catch those base-class exceptions and all related subclass exceptions. For example, an except handler that specifies the base class Exception can catch objects of any subclass of Exception. Placing an except handler that catches type Exception before other except handlers is a logic error, because all exceptions would be caught before other exception handlers could be reached. Thus, subsequent exception handlers are unreachable.

Custom Exception Classes

When you raise an exception from your code, you should generally use one of the existing exception classes from the Python Standard Library. However, using the inheritance techniques you learned earlier in this chapter, you can create your own custom exception classes that derive directly or indirectly from class Exception. Generally, that’s discouraged, especially among novice programmers. Before creating custom exception classes, look for an appropriate existing exception class in the Python exception hierarchy. Define new exception classes only if you need to catch and handle the exceptions differently from other existing exception types. That should be rare.

tick mark Self Check

  1. (Fill-In) Most exceptions you’ll encounter inherit from base class _________ and are defined in module _________.
    Answer: Exception, exceptions.

  2. (True/False) When you raise an exception from your code, you should generally use a new exception class.
    Answer: False. When you raise an exception from your code, you should generally use one of the existing exception classes from the Python Standard Library.