Built-in Exceptions
This section describes the exceptions that Python might 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). Built-in exceptions are provided in the built-in scope namespace. Many built-in exceptions have associated state information that provides exception details.
Superclasses (Categories)
The following exceptions are used only as superclasses for other exceptions.
BaseException
The root superclass for all built-in exceptions. It is not meant to be directly inherited by user-defined classes; use
Exception
for this role instead. Ifstr()
is called on an instance of this class, the representation of the constructor argument(s) passed when creating the instance are returned (or the empty string if there were no such arguments). These instance constructor arguments are stored and made available in the instance’sargs
attribute as a tuple. Subclasses inherit this protocol.Exception
The root superclass for all built-in and non-system-exiting exceptions. This is a direct subclass of
BaseException
.User-defined exceptions should inherit (be derived) from this class. This derivation is required for user-defined exceptions in Python 3.0; Python 2.6 requires this of new-style classes, but also allows standalone exception classes.
try
statements that catch this exception will catch all but system exit events, because this class is superclass to all exceptions butSystemExit
,KeyboardInterrupt ...
Get Python Pocket Reference, 4th Edition 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.