types of exceptions in Python

Types of Exceptions in Python

In the previous chapters, we looked at Errors and Exceptions in Python and Exception Handling in Python. In this chapter, we will discuss the Types of Exceptions in Python. As discussed in the previous chapters, Python has several built-in exceptions. Users can also create their exceptions, which are called user-defined exceptions, which we will discuss in the next chapter.


Link to all chapters in our Python tutorial series – Learn Python


Types of Exceptions in Python

Exception NameDescription
ZeroDivisionErrorOccurs when any number is divided by zero.
ValueErrorOccurs when a function or built-in operation gets an argument with right datatype but incorrect value.
UnboundLocalErrorOccurs when a reference is made to local variable in a method or function and when no value has been bound to that variable.
TypeErrorOccurs when an function or operation is applied to an object of inappropriate datatype.
SystemExitOccurs due to sys.exit() function. If it is not handled, the Python interpreter quits.
IndentationErrorOccurs when the indentation is not followed properly.
SyntaxErrorOccurs when compiler meets with a syntax error.
StopIterationOccurs due to iterator’s next() method to alert that there are no further elements.
RuntimeErrorOccurs when an error that does not fall under any type is raised.
OverflowErrorOccurs when the outcome of an arithmetic operation is so large that it cannot be represented.
NotImplementedErrorIt is derived from ‘RuntimeError’. In user defined base classes, the abstract methods should raise this exception when they need derived classes to override the method.
NameErrorOccurs when an identifier is missing globally or locally.
KeyboardInterruptOccurs when user prompts the interrupt key from the keyboard. (Delete or Control-C)
KeyErrorOccurs when a mapping(dictionary) key is missing in a set of available keys.
IndexErrorOccurs when a sequence index or subscript is out of range.
ImportErrorOccurs when an import statement cannot find the module that is being imported.
IOErrorOccurs during the failure of an input or output operation.
FloatingPointErrorOccurs when the operation of a floating point fails.
EOFErrorOccurs when input() function reaches end of file condition without reading any data.
AttributeErrorOccurs when an attribute reference or assignment fails.
AssertionErrorOccurs when an assert statement gives an error
ArithmeticErrorRefers to the base class for arithmetic errors such as FloatingPointError, ZeroDivisionError, OverflowError.
ExceptionRefers to any kind of Exception. All exceptions are subclasses of this Exception class.

Let us now see the occurrence of SyntaxError in a Python program.

#python program to show the syntax error
try:
  date = eval(input("Enter date: "))
except SyntaxError:
  print("You have entered invalid date.")
else:
  print("You entered: ",date)
Output 1:

Enter date: 2018, 11, 5
You entered:  (2018, 11, 5)

Output 2:

Enter date: 2018, 5c, 3
You have entered invalid date.

In the program above, the eval() function accepts input in the form of a dictionary, tuple or a list and evaluates the input appropriately. When we entered numbers separated by commas, the eval() function evaluated the input as a tuple. However, when we entered letters also with numbers, the SyntaxError exception was raised in the try block.

Scroll to Top