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.
BaseExceptionThe root superclass for all built-in exceptions. It is not meant to be directly inherited by user-defined classes; use
Exceptionfor 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’sargsattribute as a tuple. Subclasses inherit this protocol.ExceptionThe 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.
trystatements that catch this exception will catch all but system exit events, because this class is superclass to all exceptions butSystemExit,KeyboardInterrupt ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access