Python's exception mechanism allows you to signal
errors in a flexible, general way, and also to handle
errors. See the
try
and raise
statements above.
A variety of exceptions are built into Python, and they are arranged in a hierarchy so that you can handle either specific individual exceptions or larger groupings of them.
Here is the current exception hierarchy. Indentation
shows the inheritance: for example, all exceptions inherit
from Exception;
ArithmeticError inherits from
StandardError; and so on.
Exception: Base class for
all exceptions.
SystemExit:
Normal program termination. This exception is not
considered an error.
StopIteration:
This is a special exception to be used with
iterators and generators; see
iterators below.
StandardError:
Base class for all exceptions that are considered errors.
ArithmeticError:
Errors from numeric operations.
FloatingPointError:
Failure in a floating point operation.
OverflowError:
A numeric operation that led to a number
too large to be represented.
ZeroDivisionError:
Attempt to divide by zero.
LookupError:
Base class for erroneous in retrieving items from a
sequence or
mapping.
IndexError:
Trying to retrieve an element of a sequence
when the index is out of range.
KeyError:
Trying to retrieve an item from a mapping
that does not contain the given key.
EnvironmentError:
Base class for external errors. For the
exceptions in this class, the associated
value is an object with the error number in its
.errno attribute and
the error message in its
.strerror attribute.
IOError:
Input/output error.
OSError:
Operating system error.
AssertionError:
An assert
statement has failed.
AttributeError:
Attempt to retrieve or set a nonexistent
attribute of an object.
ImportError:
Attempt to import a nonexistent module, or a
nonexistent name from a module.
KeyboardInterrupt:
Someone pressed interrupt (e.g., control-C
under Unix.
MemoryError:
No more memory is available.
NameError:
Reference to an undefined name.
NotImplementedError:
Use this exception for functions that aren't
written yet. It's also recommended for virtual
methods in base classes, those intended to be
overridden.
SyntaxError:
Attempt to import or execute syntactically
invalid Python source code.
SystemError:
An internal error in Python.
TypeError:
Attempt to use an operation that isn't defined
for objects of the type it's to operate
on.
ValueError:
Attempt to operate on an inappropriate
value.