The following exceptions are part of the Python environment. There
are three broad categories of exceptions.
-
Non-error Exceptions. These are exceptions that define events
and change the sequence of execution.
-
Run-time Errors. These exceptions can occur in the normal
course of events, and indicate typical program problems.
-
Internal or Unrecoverable Errors. These exceptions occur when
compiling the Python program or are part of the internals of the
Python interpreter; there isn't much recovery possible, since it
isn't clear that our program can even continue to operate. Problems
with the Python source are rarely seen by application programs,
since the program isn't actually running.
Here are the non-error exceptions. Generally, you will never have
a handler for these, nor will you ever raise them with a
raise
statement.
-
StopIteration
-
This is raised by an iterator when there is no next value.
The
for
statement handles this to end an
iteration loop cleanly.
-
GeneratorExit
-
This is raised when a generator is closed by having the
close
method evaluated.
-
KeyboardInterrupt
-
This is raised when a user hits control-
C
to send an interrupt signal to the
Python interpreter. Generally, this is not caught in application
programs because it's the only way to stop a program that is
misbehaving.
-
SystemExit
-
This exception is raised by the
sys.exit
function. Generally, this is not
caught in application programs; this is used to force a program to
exit.
Here are the errors which can be meaningfully handled when a
program runs.
-
AssertionError
-
Assertion failed. See the
assert
statement for more information in the section called “The
assert
Statement”
-
AttributeError
-
Attribute not found in an object.
-
EOFError
-
Read beyond end of file.
-
FloatingPointError
-
Floating point operation failed.
-
IOError
-
I/O operation failed.
-
IndexError
-
Sequence index out of range.
-
KeyError
-
Mapping key not found.
-
OSError
-
OS system call failed.
-
OverflowError
-
Result too large to be represented.
-
TypeError
-
Inappropriate argument type.
-
UnicodeError
-
Unicode related error.
-
ValueError
-
Inappropriate argument value (of correct type).
-
ZeroDivisionError
-
Second argument to a division or modulo operation was
zero.
The following errors indicate serious problems with the Python
interepreter. Generally, you can't do anything if these errors should be
raised.
-
MemoryError
-
Out of memory.
-
RuntimeError
-
Unspecified run-time error.
-
SystemError
-
Internal error in the Python interpreter.
The following exceptions are more typically returned at compile
time, or indicate an extremely serious error in the basic construction
of the program. While these exceptional conditions are a necessary part
of the Python implementation, there's little reason for a program to
handle these errors.
-
ImportError
-
Import can't find module, or can't find name in
module.
-
IndentationError
-
Improper indentation.
-
NameError
-
Name not found globally.
-
NotImplementedError
-
Method or function hasn't been implemented yet.
-
SyntaxError
-
Invalid syntax.
-
TabError
-
Improper mixture of spaces and tabs.
-
UnboundLocalError
-
Local name referenced but not bound to a value.
The following exceptions are part of the implementation of
exception objects. Normally, these never occur directly. These are
generic categories of exceptions. When you use one of these names in a
catch
clause, a number of more more specialized
exceptions will match these.
-
Exception
-
Common base class for all user-defined exceptions.
-
StandardError
-
Base class for all standard Python errors. Non-error
exceptions (StopIteration
,
GeneratorExit
,
KeyboardInterrupt
and
SystemExit
) are not subclasses of
StandardError
.
-
ArithmeticError
-
Base class for arithmetic errors. This is the generic
exception class that includes
OverflowError
,
ZeroDivisionError
, and
FloatingPointError
.
-
EnvironmentError
-
Base class for errors that are input-output or operating
system related. This is the generic exception class that includes
IOError
and
OSError
.
-
LookupError
-
Base class for lookup errors in sequences or mappings, it
includes IndexError
and
KeyError
.