Professional Documents
Culture Documents
Exceptions in Python: Different Types of Exceptions and How To Handle Them in Python
Exceptions in Python: Different Types of Exceptions and How To Handle Them in Python
Exceptions can either belong to the in-built errors/exceptions or have custom exceptions. Some
of the common in-built exceptions are as follows:
1. ZeroDivisionError
2. NameError
3. IndentationError
4. IOError
5. EOFError
The output when the numerator is an integer and the denominator is given as 0 is shown below.
Enter numerator: 2
Enter denominator: 0
a/b results in :
Traceback (most recent call last):
File "C:/Users/Hp/Desktop/test.py", line 4, in <module>
print(a/b)
ZeroDivisionError: division by zero
try:
#block of code
except <Name of Exception>:
#block of code
#Rest of the code
1try:
2 a = int(input("Enter numerator: "))
3 b = int(input("Enter denominator: "))
4 print(a/b)
5except ZeroDivisionError:
print("Denominator is zero")
6
The output of this code for the same inputs as before is shown below.
Enter numerator: 2
Enter denominator: 0
Denominator is zero