Oct. 10, 2012

Python's Built-In Exceptions

Python's Built-In Exceptions


The base class for all built-in exceptions.


All built-in, non-system-exiting exceptions are derived from this class. 

All user-defined exceptions should also be derived from this class.


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


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


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()


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


Raised when an assert statement fails.


Raised when an attribute reference or assignment fails.


Raised when one of the built-in functions (input() or raw_input()) hits an 
end-of-file condition (EOF) without reading any data. 


Raised when a floating point operation fails.


Raise when a generator's close() method is called. 

It directly inherits from Exception instead of StandardError since it is
technically not an error.


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.


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.


Raised when a sequence subscript is out of range. 


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


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


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


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.


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.


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


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


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. 


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


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


Raised when the parser encounters a syntax error.


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).


This exception is raised by the sys.exit() function. 

When it is not handled, the Python interpreter exits; no stack traceback is 

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.


Raised when an operation or function is applied to an object of inappropriate

The associated value is a string giving details about the type mismatch.


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


Raised when a Unicode-related encoding or decoding error occurs.

It is a subclass of ValueError.


Raised when a Unicode-related error occurs during encoding. 

It is a subclass of UnicodeError.


Raised when a Unicode-related error occurs during decoding. 

It is a subclass of UnicodeError.


Raised when a Unicode-related error occurs during translating. 

It is a subclass of UnicodeError.


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.


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


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

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.