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
:
SystemExit
terminates program execution (or terminates an interactive session) and when uncaught does not produce a traceback like other exception types.
KeyboardInterrupt
exceptions occur when the user types the interrupt command—Ctrl + C (or control + C) on most systems.
GeneratorExit
exceptions occur when a generator closes—normally when a generator finishes producing values or when its close
method is called explicitly.
Exception
is the base class for most common exceptions you’ll encounter. You’ve seen exceptions of the Exception
subclasses ZeroDivisionError
, NameError
, ValueError
, StatisticsError
, TypeError
, IndexError
, KeyError
, RuntimeError
and AttributeError
. Often, StandardError
s can be caught and handled, so the program can continue running.
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.
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.
(Fill-In) Most exceptions you’ll encounter inherit from base class _________ and are defined in module _________.
Answer: Exception
, exceptions
.
(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.