Built-in Exceptions

This section describes the exceptions that Python may raise during a program’s execution. Beginning with Python 1.5, all built-in exceptions are class objects. Prior to 1.5, they were strings. Class exceptions are mostly indistinguishable from strings, unless they are concatenated. Built-in exceptions are defined in the module exceptions; this module never needs to be imported explicitly, because the exception names are provided in the built-in scope namespace. Most built-in exceptions have an associated extra data value with details.

Base Classes (Categories)

Exception

Root superclass for all exceptions. User-defined exceptions may be derived from this class, but this is not currently enforced or required.

StandardError

Superclass for all other built-in exceptions except for SystemExit; subclass of the Exception root class.

ArithmeticError

Superclass for OverflowError, ZeroDivisionError, FloatingPointError; subclass of StandardError.

LookupError

Superclass for IndexError, KeyError; subclass of StandardError.

EnvironmentError

Superclass for exceptions that occur outside Python (IOError, OSError); subclass of StandardError. New in Release 1.5.2.

Specific Exceptions Raised

AssertionError

When an assert statement’s test is false.

AttributeError

On attribute reference or assignment failure.

EOFError

When immediate end of file is hit by input( ) or raw_input( ).

FloatingPointError

On floating-point operation failure.

IOError

On I/O or file-related operation failure.

ImportError ...

Get Python Pocket Reference, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.