Built-in Exceptions in Python

An exception is a runtime event, that occurs during the execution of a program and disrupts the normal flow of the program’s instructions. An exception is a Python object that represents an error. If the user code contains an error, it will raise an exception. We can use this to test an exception handler or to report an error condition when the interpreter raises the same exception. However, We cannot prevent user code from raising an exception.
The built-in exceptions listed below can be generated by the interpreter or built-in functions.

Exception Description
ArithmeticError Raised when an error occurs in numeric calculations
AssertionError Raised when an assert statement fails.
AttributeError Raised when attribute reference or assignment fails.
EOFError Raised when the input() function hits the end-of-file condition without reading any data. The read() and readline() methods return an empty string when they hit EOF.
FloatingPointError This Exception is not currently used. Raised when a floating point operation fails.
GeneratorExit Raise when a close method of generator or coroutine is called.
ImportError Raised when the import statement has troubles trying to load a module.
IndentationError Raised when indentation is incorrect.
IndexError Raised when the index of a sequence does not exist.
KeyError Raised when a mapping key is not found in a dictionary.
KeyboardInterrupt Raised when the user hits the interrupt key by pressing key Ctrl+C or Delete.
ModuleNotFoundError From python 3.6 onwards, Raised when a module cannot be located.
MemoryError Raised when an operation runs out of memory but the situation may still be rescued by deleting some objects.
NameError Raised when a variable is not found in local or global scope.
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, or while the class is being developed to indicate that the real implementation still needs to be added.
OSError Raised when system operation causes system-related error including I/O failures such as “file not found” or “disk full”.
OverflowError Raised when the result of an arithmetic operation is too large to be represented.
RecursionError From Python 3.5 onwards, It is raised when the interpreter detects that the maximum recursion depth is exceeded.
ReferenceError Raised when a weak reference proxy is used to access a garbage collected referent.
RuntimeError Raised when an error does not fall under any of the other categories.
StopIteration Raised by built-in functionnext() function to indicate that there is no further item to be returned by iterator.
StopAsyncIteration From python 3.5 onwards, Raised by anext() method of an asynchronous iterator object to stop the iteration.
SyntaxError Raised by parser when syntax error is encountered.
TabError Raised when indentation consists of inconsistent use of tabs and spaces.
SystemError Raised when interpreter detects an internal error, but the situation does not look so serious.
SystemExit Raised by sys.exit() function. This allows the exception to properly propagate up and cause the interpreter to exit.
TypeError Raised when a function or operation is applied to an object of inappropriate type.
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.
UnicodeError Raised when a Unicode-related encoding or decoding error occurs.
UnicodeEncodeError Raised when a Unicode-related error occurs during encoding.
UnicodeDecodeError Raised when a Unicode-related error occurs during decoding.
UnicodeTranslateError Raised when a Unicode-related error occurs during translating.
ValueError Raised when an 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
ZeroDivisionError Raised when the second argument of a division or modulo operation is zero.
BlockingIOError Raised when an operation would block on an object (e.g. socket) set for non-blocking operation.
ChildProcessError Raised when an operation on a child process failed.
ConnectionError Raised when there is a connection-related issues.
FileExistsError Raised when trying to create a file or directory which already exists
FileNotFoundError Raised when a file or directory is requested but doesn’t exist.
InterruptedError Raised when a system call is interrupted by an incoming signal.
IsADirectoryError Raised when a file operation such as os.remove() is requested on a directory.
NotADirectoryError Raised when a directory operation such as os.listdir() is requested on something which is not a directory.
PermissionError Raised when trying to run an operation without the adequate access rights – for example filesystem permissions.
ProcessLookupError Raised when a given process doesn’t exist.
TimeoutError Raised when a system function timed out at the system level.


Happy Learning 🙂

Leave A Comment