
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to catch ZeroDivisionError Exception in Python?
ZeroDivisionError Exception in Python
The ZeroDivisionError is raised in Python when a number is divided by zero. Since dividing by zero is mathematically undefined, Python throws this error.
In this article, you will learn how to catch and handle ZeroDivisionError using the try-except block. Usually, when an exception occurs, the program will be terminated abruptly, leaving the statements after the exception unexecuted.
By handling this exception, the execution of the program continues even after the ZeroDivisionError exception (an invalid division attempt). The ZeroDivisionError generally occurs in the following cases -
- Dividing any number by zero using / or // operators
- Modulo operation with zero as the divisor using %
Using try-except to Catch ZeroDivisionError
We can catch ZeroDivisionError by placing the code that is prone to this exception in the try-except block. In the except block, we can print an appropriate message providing the required information about the exception.
Example
In this example, we try to divide a number by zero, and catch the exception using ZeroDivisionError -
try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero.")
Following is the output obtained -
Cannot divide by zero.
Catching ZeroDivisionError Using Exception Object
You can also use the exception object to display the exact error message generated by Python.
Example
In this example, we catch the error and print its message using the exception object -
try: value = 8 // 0 except ZeroDivisionError as e: print("Error caught:", e)
We get the output as shown below -
Error caught: integer division or modulo by zero
Using ZeroDivisionError in User Input
Typically, while performing arithmetic operations like division, we accept values from the user.
In this example, we are accepting the denominator value for the user and placing the statements performing the division operation within the try-except block -
try: num = int(input("Enter a number: ")) result = 100 / num print("Result is:", result) except ZeroDivisionError: print("Cannot divide by zero.")
The error obtained is as shown below -
Enter a number: 0 Cannot divide by zero.
Handling Multiple Exceptions
You can catch multiple exceptions (such as ValueError and ZeroDivisionError) in one program by using multiple except blocks.
Example
In the following example, we handle both invalid input and division by zero separately -
try: num = int(input("Enter a number: ")) result = 50 / num print("Result:", result) except ValueError: print("Invalid input. Please enter a number.") except ZeroDivisionError: print("Cannot divide by zero.")
The result produced is as follows -
Enter a number: 0 Cannot divide by zero.