When a programmer does coding, there are possibilities he can get errors. There are two types of errors in Python syntax error and exception error. A syntax error occurs when source code translates into byte code and lets the programmer know that the program contains the wrong syntax. i.e. SyntaxError: invalid syntax.
Exception error occurs at the time of execution. It stops the implementation of the program because an interruption occurred in the program.
In this article, we are going to discuss Python Exception Handling i.e. Python try except. Many types of exceptions occurs in a Python program. Like:
- ZeroDivisionError: It will occur when we try to divide a number with zero.
- NameError: It will occur when it cannot find the name.
- IOError: It will occur when input-output operations failed.
- ValueError: It occurs when built-in functions get the wrong argument.
- And More
We can handle these types of exceptions using Python try and except blocks. Try block will check the code and catch the error, and it will not execute the code. Whereas, except block will encounter the error and proceed to the try block section. So, let’s create some programs and handle these types of Python exceptions.
Table of Contents
1. Python ZeroDivisionError
If you have basic knowledge of mathematics division, you must know that we cannot divide a number with zero, or if we try to do so, we will get infinity in the answer. So, if we try to divide any number with zero in a Python program, it will throw an exception error that is “ZeroDivisionError.”
Example Code: Before handling the ZeroDivisionError
num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) div = num1/num2 print("Division = ", div)
You can see in above example we tried to divide a number with zero and got error exception “ZeroDivisionError”.
Example Code: After handling the ZeroDivisionError
num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) try: div = num1/num2 except: print("You cannot divide a number with zero")
So, we handled the exception error “ZeroDivisionError.” We can handle all types of exceptions in this way by using try and except blocks.
2. Python NameError
NameError occurs when the program searches for the name that is not declared or initialized in the code. If we try to find a name that does not exist, it will throw an exception error “NameError.”
Example Code: Before handling the NameError
sum = x + 1 print(sum)
Example Code: After handling the NameError
try: sum = x + 1 except: print("Name x is not defined")
3. Python IOError
This error occurs when input/output operations fail. For example, we are trying to open a file that does not exist in the system. It can throw this error on any of the functions like print and open in this example.
Example Code: Before handling the IOError
import sys def readFile(): f = open ( "xyz.txt", 'r' ) print(f) readFile()
Example Code: After handling the IOError
import sys def readFile(): try: f = open ( "xyz.txt", 'r' ) except: print("IOError Occur") readFile()
4. Python ValueError
The ValueError occurs when a programmer or a user enters the wrong value in a function. For example, if we enter a negative value in the math module’s function sqrt(), which use to find the mathematical square root of a number, it will throw “ValueError.”
Example Code: Before handling the ValueError
import math s_r_o_n = math.sqrt(-9) print(s_r_o_n)
Example Code: After handling the ValueError
import math try: s_r_o_n = math.sqrt(-9) except: print("Cannot find square root of any negative value")
In this tutorial, we discussed Python Exception handling using try and except blocks. There are many types of exception errors in Python, and we debated only four of them. We created the situations to the occurrence of these exception errors so that we get to know in what conditions they occur, and also we saw how we could handle them using try and except blocks.