Source

sandbox/morph / Doc / library / exceptions.rst

Built-in Exceptions

Exceptions should be class objects. The exceptions are defined in the module :mod:`exceptions`. This module never needs to be imported explicitly: the exceptions are provided in the built-in namespace as well as the :mod:`exceptions` module.

For class exceptions, in a :keyword:`try` statement with an :keyword:`except` clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which it is derived). Two exception classes that are not related via subclassing are never equivalent, even if they have the same name.

The built-in exceptions listed below can be generated by the interpreter or built-in functions. Except where mentioned, they have an "associated value" indicating the detailed cause of the error. This may be a string or a tuple containing several items of information (e.g., an error code and a string explaining the code). The associated value is the second argument to the :keyword:`raise` statement. If the exception class is derived from the standard root class :exc:`BaseException`, the associated value is present as the exception instance's :attr:`args` attribute.

User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition "just like" the situation in which the interpreter raises the same exception; but beware that there is nothing to prevent user code from raising an inappropriate error.

The built-in exception classes can be sub-classed to define new exceptions; programmers are encouraged to at least derive new exceptions from the :exc:`Exception` class and not :exc:`BaseException`. More information on defining exceptions is available in the Python Tutorial under :ref:`tut-userexceptions`.

The following exceptions are only used as base classes for other exceptions.

The following exceptions are the exceptions that are actually raised.

The following exceptions are used as warning categories; see the :mod:`warnings` module for more information.

Exception hierarchy

The class hierarchy for built-in exceptions is: