Python defines a complete hierarchy of built-in exception classes. When you write a handler, you can specify any class in this hierarchy, and that handler will apply to that class and any derived classes. This allows you to write generic handlers that catch whole groups of exception types.
In describing this hierarchy, we will use indentation to
show the subclass/parent class relationships. Generally
raise statement will name one of the
“leaf” classes, that is, a class that does
not have any subclasses. Ancestor classes that are not
usually raised are marked with
an asterisk (*) in the section below.
BaseException*: This is the ancestor
of all exception classes. The constructor for this
class takes one argument, a string containing the
error message. That argument is available as the
SystemExit: Raise this exception
to terminate execution of your program; it is a
special case in that it does not produce a stack
traceback. In module
.exit() method raises this
It is possible to write a handler for this
exception. To defeat such a handler and force
immediate termination, import module
os and use method
KeyboardInterrupt: Raised when the
user signals an interruption with the keyboard
(del under Windows, or Control-C in Linux or Mac environments).
This class inherits from
BaseException rather than from
Exception so that catch-all handlers for
Exception will not prevent
Exception*: This is the preferred
base class for all built-in and user-defined
exceptions. If you want to write a handler for
all these exception types, use
Exception as the handler's type.
>>> try: ... x = 1 / 0 ... except Exception, detail: ... print "Fail:", detail.message ... Fail: integer division or modulo by zero >>> try: ... x = noSuchVariable ... except Exception, detail: ... print "Fail:", detail.message ... Fail: name 'noSuchVariable' is not defined
A catch-all handler like this can mask any number of errors. Do not use such a handler unless your program must absolutely stay running.
StopIteration: This is the
exception that a generator must raise in
order to signal that no more generated values
are available. See Section 24.3, “Generators: Functions that can produce a sequence of
StandardError*: This is the base
class for all built-in exceptions that are
ArithmeticError*: This is
the base class for errors involving
FloatingPointError: This is
raised for arithmetic errors
OverflowError: This is
raised when the result of an
operation cannot be represented.
attempt to divide by zero.
assert statement has failed.
See Section 22.2, “The
assert statement: Verify
AttributeError: Failure to
access an attribute.
caused by functions outside of Python,
such as the operating system or
related to file input or output.
OSError: Errors signaled
from the operating system.
ImportError: Failure to
import a module or to import items from a
LookupError*: Superclass for
errors caused by attempts to retrieve
values from inside a container class.
IndexError: Attempt to
retrieve a sequence member
is not a
valid index in sequence
KeyError: Attempt to
retrieve a dictionary member
is not a
valid key in
MemoryError: No more
processor memory is available.
NameError: Attempt to
retrieve a name that is not defined.
Attempt to retrieve the value of a local
name when no value has yet been
assigned to it.
RuntimeError: An error
that doesn't fit the other categories.
This is the preferred way for the
virtual methods of a base class to
signal that they have not been
replaced by a concrete method in a
SyntaxError: Attempt to
execute invalid Python source code.
TypeError: Attempt to
perform an operation on a value that does
not support that operation, such as
trying to use exponentiation (
**) on a string.
ValueError: Caused by an
operation that is performed on values of
the correct type, but the actual values
are not valid. Example: taking a
negative number to a fractional power.