Predefined Exception Classes

The following exceptions are raised by programs:


Failed assert statement.


Failed attribute reference or assignment. EOFError

End of file. Generated by the built-in functions input() and raw_input(). It should be noted that most other I/O operations such as the read() and readline() methods of files return an empty string to signal EOF instead of raising an exception.


Failed floating-point operation. It should be noted that floating-point exception-handling is a tricky problem and only that this exception only gets raised if Python has been configured and built in a way that enables it. It is more common for floating-point errors to silently produce results such as float('nan') or float('inf'). A subclass of ArithmeticError.


Raised inside a generator function to signal termination. This happens when a generator is destroyed prematurely (before all generator values are consumed) or the close() method of a generator is called. If a generator ignores this exception, the generator is terminated and the exception is silently ignored.


Failed I/O operation.The value is an IOError instance with the attributes errno, strerror, and filename. errno is an integer error number, strerror is a string error message, and filename is an optional filename. A subclass of EnvironmentError.


Raised when an import statement can't find a module or when from can't find a name in a module.


Indentation error. A subclass of SyntaxError. IndexError

Sequence subscript out of range. A subclass of LookupError. KeyError

Key not found in a mapping. A subclass of LookupError. KeyboardInterrupt

Raised when the user hits the interrupt key (usually Ctrl+C). MemoryError

Recoverable out-of-memory error. NameError

Name not found in local or global namespaces. NotImplementedError

Unimplemented feature. Can be raised by base classes that require derived classes to implement certain methods. A subclass of RuntimeError.


Operating system error. Primarily raised by functions in the os module. The value is the same as for IOError. A subclass of EnvironmentError.


Result of an integer value being too large to be represented. This exception usually only arises if large integer values are passed to objects that internally rely upon fixed-precision machine integers in their implementation. For example, this error can arise with range or xrange objects if you specify starting or ending values that exceed 32 bits in size. A subclass of ArithmeticError.


Result of accessing a weak reference after the underlying object has been destroyed. See the weakref module.


A generic error not covered by any of the other categories.


Raised to signal the end of iteration. This normally happens in the next() method of an object or in a generator function.


Parser syntax error. Instances have the attributes filename, lineno, offset, and text, which can be used to gather more information.


Internal error in the interpreter. The value is a string indicating the problem.


Raised by the sys.exit() function. The value is an integer indicating the return code. If it's necessary to exit immediately, os._exit() can be used.


Inconsistent tab usage. Generated when Python is run with the -tt option. A subclass of SyntaxError.


Occurs when an operation or a function is applied to an object of an inappropriate type.


Unbound local variable referenced. This error occurs if a variable is referenced before it's defined in a function. A subclass of NameError.


Unicode encoding or decoding error. A subclass of ValueError. UnicodeEncodeError

Unicode encoding error. A subclass of UnicodeError. UnicodeDecodeError

Unicode decoding error. A subclass of UnicodeError. UnicodeTranslateError

Unicode error occurred during translation. A subclass of UnicodeError. ValueError

Generated when the argument to a function or an operation is the right type but an inappropriate value.


Generated by failed system calls on Windows. A subclass of OSError. ZeroDivisionError

Dividing by zero. A subclass of ArithmeticError.

Was this article helpful?

0 0

Post a comment