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
a 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
.message attribute.
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 sys, the
.exit() method raises this
exception.
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 os._exit().
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
class Exception will not prevent
program termination.
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
values”.
StandardError*: This is the base
class for all built-in exceptions that are
considered errors.
ArithmeticError*: This is
the base class for errors involving
arithmetic computations.
FloatingPointError: This is
raised for arithmetic errors
involving the float
type.
OverflowError: This is
raised when the result of an
operation cannot be represented.
ZeroDivisionError: An
attempt to divide by zero.
AssertionError: An assert statement has failed.
See Section 22.2, “The assert statement: Verify
preconditions”.
AttributeError: Failure to
access an attribute.
EnvironmentError*: Errors
caused by functions outside of Python,
such as the operating system or
peripheral devices.
IOError: Errors
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
module.
LookupError*: Superclass for
errors caused by attempts to retrieve
values from inside a container class.
IndexError: Attempt to
retrieve a sequence member , where S[I] is not a
valid index in sequence I.
S
KeyError: Attempt to
retrieve a dictionary member , where D[K] is not a
valid key in K.
D
MemoryError: No more
processor memory is available.
NameError: Attempt to
retrieve a name that is not defined.
UnboundLocalError:
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.
NotImplementedError:
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
derived class.
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.