Exceptions in Python

When writing a program, we, more often than not, will encounter errors. Error caused by not following the proper structure (syntax) of the language is called syntax error or parsing error.
>>> if a < 3 File "", line 1 if a < 3 ^ SyntaxError: invalid syntax
We can notice here that a colon is missing in the if statement. Errors can also occur at runtime and these are called exceptions. They occur, for example, when a file we try to open does not exist (FileNotFoundError), dividing a number by zero (ZeroDivisionError), module we try to import is not found (ImportError) etc. Whenever these type of runtime error occur, Python creates an exception object. If not handled properly, it prints a traceback to that error along with some details about why that error occurred.
>>> 1 / 0 Traceback (most recent call last): File "", line 301, in runcode File "", line 1, in ZeroDivisionError: division by zero >>> open("imaginary.txt") Traceback (most recent call last): File "", line 301, in runcode File "", line 1, in FileNotFoundError: [Errno 2] No such file or directory: 'imaginary.txt'
Python Built-in Exceptions Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that are raised when corresponding errors occur. We can view all the built-in exceptions using the local() built-in functions as follows. >>> locals()['__builtins__'] This will return us a dictionary of built-in exceptions, functions and attributes. Some of the common built-in exceptions in Python programming along with the error that cause then are tabulated below.

Python Built-in Exceptions

AssertErrorRaised when assert statement fails.
AttributeError Raised when attribute assignment or reference fails.
EOFErrorRaised when the input() functions hits end-of-file condition.
FloatingPointErrorRaised when a floating point operation fails.
GeneratorExit Raise when a generator's close() method is called.
ImportError Raised when the imported module is not found.
IndexError Raised when index of a sequence is out of range.
KeyErrorRaised when a key is not found in a dictionary.
KeyboardInterruptRaised when the user hits interrupt key (Ctrl+c or delete).
MemoryErrorRaised when an operation runs out of memory.
NameError Raised when a variable is not found in local or global scope.
NotImplementedErrorRaised by abstract methods.
OSErrorRaised when system operation causes system related error.
OverflowError Raised when result of an arithmetic operation is too large to be represented.
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 other category..
StopIterationRaised by next() function to indicate that there is no further item to be returned by iterator.
SyntaxErrorRaised by parser when syntax error is encountered.
IndentationError Raised when there is incorrect indentation.
TabError Raised when indentation consists of inconsistent tabs and spaces.
TSystemError Raised when interpreter detects internal error.
SystemExit Raised by sys.exit() function.
TypeErrorRaised when a function or operation is applied to an object of incorrect type.
OSErrorRaised 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.
UnicodeTranslateErrorRaised when a Unicode-related error occurs during translating.
ValueErrorRaised when a function gets argument of correct type but improper value.
ZeroDivisionError Raised when second operand of division or modulo operation is zero.