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)


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


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


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


Superclass for IndexError, KeyError; subclass of StandardError.


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

Specific Exceptions Raised


When an assert statement’s test is false.


On attribute reference or assignment failure.


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


On floating-point operation failure.


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.