Python/User-defined Exceptions
[i]Learn· Errors and Exceptions

User-defined Exceptions

Programs may name their own exceptions by creating a new exception class (see Classes for more about Python classes). Exceptions should typically be derived from the Exception class, either directly or indirectly.

Exception classes can be defined which do anything any other class can do, but are usually kept simple, often only offering a number of attributes that allow information about the error to be extracted by handlers for the exception.

Most exceptions are defined with names that end in “Error”, similar to the naming of the standard exceptions.

Many standard modules define their own exceptions to report errors that may occur in functions they define.

Source: Errors and Exceptions by Python Software Foundation, licensed under PSF-2.0. Retrieved 2026-11-18. Changes: Extracted section 8.6 from The Python Tutorial (Python 3.14), https://docs.python.org/3.14/tutorial/errors.html; removed section numbers, permalinks and site navigation; code blocks kept as shown.