Oct. 10, 2012

Python's Built-In Exceptions

Python's Built-In Exceptions

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

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.

EOFError

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

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.

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. 

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. 

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

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.