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.
|Raised when an error occurs in numeric calculations
|Raised when an assert statement fails.
|Raised when attribute reference or assignment fails.
|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.
|This Exception is not currently used. Raised when a floating point operation fails.
|Raise when a close method of generator or coroutine is called.
|Raised when the import statement has troubles trying to load a module.
|Raised when indentation is incorrect.
|Raised when the index of a sequence does not exist.
|Raised when a mapping key is not found in a dictionary.
|Raised when the user hits the interrupt key by pressing key Ctrl+C or Delete.
|From python 3.6 onwards, Raised when a module cannot be located.
|Raised when an operation runs out of memory but the situation may still be rescued by deleting some objects.
|Raised when a variable is not found in local or global scope.
|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.
|Raised when system operation causes system-related error including I/O failures such as “file not found” or “disk full”.
|Raised when the result of an arithmetic operation is too large to be represented.
|From Python 3.5 onwards, It is raised when the interpreter detects that the maximum recursion depth is exceeded.
|Raised when a weak reference proxy is used to access a garbage collected referent.
|Raised when an error does not fall under any of the other categories.
|Raised by built-in functionnext() function to indicate that there is no further item to be returned by iterator.
|From python 3.5 onwards, Raised by anext() method of an asynchronous iterator object to stop the iteration.
|Raised by parser when syntax error is encountered.
|Raised when indentation consists of inconsistent use of tabs and spaces.
|Raised when interpreter detects an internal error, but the situation does not look so serious.
|Raised by sys.exit() function. This allows the exception to properly propagate up and cause the interpreter to exit.
|Raised when a function or operation is applied to an object of inappropriate type.
|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.
|Raised when a Unicode-related error occurs during encoding.
|Raised when a Unicode-related error occurs during decoding.
|Raised when a Unicode-related error occurs during translating.
|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
|Raised when the second argument of a division or modulo operation is zero.
|Raised when an operation would block on an object (e.g. socket) set for non-blocking operation.
|Raised when an operation on a child process failed.
|Raised when there is a connection-related issues.
|Raised when trying to create a file or directory which already exists
|Raised when a file or directory is requested but doesn’t exist.
|Raised when a system call is interrupted by an incoming signal.
|Raised when a file operation such as os.remove() is requested on a directory.
|Raised when a directory operation such as os.listdir() is requested on something which is not a directory.
|Raised when trying to run an operation without the adequate access rights – for example filesystem permissions.
|Raised when a given process doesn’t exist.
|Raised when a system function timed out at the system level.
Happy Learning 🙂