1. Technology

Python's Built-In Exceptions


Warning Sign - ERROR
porcorex/E+/Getty Images

The following is a complete list of Python errors that can be raised. While most of these are self-explanatory, I have added brief descriptions to them for sake of clarity; these are derived from the "Built-in Exceptions" module of the Python 2.5 documentation.

  • BaseException: The base class for all built-in exceptions.

  • Exception: All built-in, non-system-exiting exceptions are derived from this class. All user-defined exceptions should also be derived from this class.

  • StandardError: The base class for all built-in exceptions except StopIteration, GeneratorExit, KeyboardInterrupt and SystemExit. StandardError itself is derived from Exception.

  • ArithmeticError: The base class for those built-in exceptions that are raised for various arithmetic errors: OverflowError, ZeroDivisionError, FloatingPointError.

  • LookupError: The base class for the exceptions that are raised when a key or index used on a mapping or sequence is invalid: IndexError, KeyError. This can be raised directly by sys.setdefaultencoding().

  • EnvironmentError: The base class for exceptions that can occur outside the Python system: IOError, OSError.

  • AssertionError: Raised when an assert statement fails.

  • AttributeError: Raised when an attribute reference or assignment fails. (When an object does not support attribute references or attribute assignments at all, TypeError is raised.)

  • EOFError: Raised when one of the built-in functions (input() or raw_input()) hits an end-of-file condition (EOF) without reading any data. (N.B.: the read() and readline() methods of file objects return an empty string when they hit EOF.)

  • FloatingPointError: Raised when a floating point operation fails.

  • GeneratorExit: Raise when a generator's close() method is called. It directly inherits from Exception instead of StandardError since it is technically not an error. New in version 2.5.

  • IOError: Raised when an I/O operation (such as a print statement, the built-in open() function or a method of a file object) fails for an I/O-related reason, e.g., ``file not found'' or ``disk full''. This class is derived from EnvironmentError.

  • ImportError: Raised when an import statement fails to find the module definition or when a from...import fails to find a name that is to be imported.

  • IndexError: Raised when a sequence subscript is out of range. (Slice indices are silently truncated to fall in the allowed range; if an index is not a plain integer, TypeError is raised.)

  • KeyError: Raised when a mapping (dictionary) key is not found in the set of existing keys.

  • KeyboardInterrupt: Raised when the user hits the interrupt key (normally Control-C or Delete).

  • MemoryError: Raised when an operation runs out of memory but the situation may still be rescued (by deleting some objects).

  • NameError: Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found.

  • NotImplementedError: This exception is derived from RuntimeError. In user defined base classes, abstract methods should raise this exception when they require derived classes to override the method.

  • OSError: This class is derived from EnvironmentError and is used primarily as the os module's os.error exception.

  • OverflowError: Raised when the result of an arithmetic operation is too large to be represented.

  • ReferenceError: This exception is raised when a weak reference proxy, created by the weakref.proxy() function, is used to access an attribute of the referent after it has been garbage collected. For more information on weak references, see the weakref module.

  • RuntimeError: Raised when an error is detected that doesn't fall in any of the other categories.

  • StopIteration: Raised by an iterator's next() method to signal that there are no further values.

  • SyntaxError: Raised when the parser encounters a syntax error.

  • SystemError: Raised when the interpreter finds an internal error, but the situation does not look so serious to cause it to abandon all hope. The associated value is a string indicating what went wrong (in low-level terms).

  • SystemExit: This exception is raised by the sys.exit() function. When it is not handled, the Python interpreter exits; no stack traceback is printed. If the associated value is a plain integer, it specifies the system exit status (passed to C's exit() function) ; if it is None, the exit status is zero; if it has another type (such as a string), the object's value is printed and the exit status is one.

  • TypeError: Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.

  • UnboundLocalError: Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable.

  • UnicodeDecodeError: Raised when a Unicode-related encoding or decoding error occurs. It is a subclass of ValueError.

  • UnicodeEncodeError: Raised when a Unicode-related error occurs during encoding. It is a subclass of UnicodeError.

  • UnicodeError: Raised when a Unicode-related error occurs during decoding. It is a subclass of UnicodeError.

  • UnicodeTranslateError: Raised when a Unicode-related error occurs during translating. It is a subclass of UnicodeError.

  • ValueError: Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.

  • WindowsError: Raised when a Windows-specific error occurs or when the error number does not correspond to an errno value.

  • ZeroDivisionError: Raised when the second argument of a division or modulo operation is zero. The associated value is a string indicating the type of the operands and the operation.

  1. About.com
  2. Technology
  3. Python
  4. Python Library
  5. Python's Built-In Exceptions

©2014 About.com. All rights reserved.