Built-in Exceptions

Exceptions which Python may raise during a program’s execution. Beginning with Python 1.5, all built-in exceptions are classes. Prior to 1.5, they were strings; to force string exceptions for backward compatibility, use the -X command-line option flag. Class exceptions are mostly indistinguishable from strings, unless concatenated.

Base Classes (Categories)

Exception

Root superclass for all exceptions.

StandardError

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

ArithmeticError

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

LookupError

Superclass for IndexError, KeyError; subclass of StandardError.

Specific Exceptions

AssertionError

Raised when an assert statement’s test is false.

AttributeError

On attribute reference or assignment failure.

EOFError

Immediate end-of-file hit by input( ) or raw_input( ).

FloatingPointError

When a floating-point operation fails.

IOError

I/O or file-related operation failure.

ImportError

On failure of import to find module or attribute.

IndexError

On out-of-range sequence offset (fetch or assign).

KeyError

On reference to non-existent mapping key (fetch).

KeyboardInterrupt

On user entry of the interrupt key (often Ctrl-C).

MemoryError

On recoverable memory exhaustion.

NameError

On failure to find a local or global unqualified name.

OverflowError

On excessively large arithmetic operation.

RuntimeError

Obsolete catch-all; define a suitable error instead.

SyntaxError

On parser encountering ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.