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 Name | Description |
ZeroDivisionError | Occurs when any number is divided by zero. |
ValueError | Occurs when a function or built-in operation gets an argument with right datatype but incorrect value. |
UnboundLocalError | Occurs when a reference is made to local variable in a method or function and when no value has been bound to that variable. |
TypeError | Occurs when an function or operation is applied to an object of inappropriate datatype. |
SystemExit | Occurs due to sys.exit() function. If it is not handled, the Python interpreter quits. |
IndentationError | Occurs when the indentation is not followed properly. |
SyntaxError | Occurs when compiler meets with a syntax error. |
StopIteration | Occurs due to iterator’s next() method to alert that there are no further elements. |
RuntimeError | Occurs when an error that does not fall under any type is raised. |
OverflowError | Occurs when the outcome of an arithmetic operation is so large that it cannot be represented. |
NotImplementedError | It 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. |
NameError | Occurs when an identifier is missing globally or locally. |
KeyboardInterrupt | Occurs when user prompts the interrupt key from the keyboard. (Delete or Control-C) |
KeyError | Occurs when a mapping(dictionary) key is missing in a set of available keys. |
IndexError | Occurs when a sequence index or subscript is out of range. |
ImportError | Occurs when an import statement cannot find the module that is being imported. |
IOError | Occurs during the failure of an input or output operation. |
FloatingPointError | Occurs when the operation of a floating point fails. |
EOFError | Occurs when input() function reaches end of file condition without reading any data. |
AttributeError | Occurs when an attribute reference or assignment fails. |
AssertionError | Occurs when an assert statement gives an error |
ArithmeticError | Refers to the base class for arithmetic errors such as FloatingPointError, ZeroDivisionError, OverflowError. |
Exception | Refers 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.