Types Of Exceptions In Python

Introduction

 
In this article, I will explain the types of exceptions in Python.
  1. Try
  2. Except
  3. Finally
The try block is used to check a block of code for errors. The except block is used to take care of the error. The finally block is used to execute code, irrespective of the result of the try and except blocks.
 

Exception handling

 
When an error occurs, or exception as we call it, Python will generally stop and generate a mistake message. The exception is handled using the try statement.
 
Syntax
  1. try:    
  2.    //statement    
  3. except:  
  4.    //statement    
 Program
  1. a=10  
  2. try:  
  3.   b=a/0 #error occurs.  
  4.   print(b)  
  5. except:  
  6.   print ("10 was not divided by 0")   
Output
 
The try block will generate an error because 10 was not divided by 0. The try block is an error so, the except block will be executed. Without the try block, the program will show the error in the output screen. So, we use try blocks.
 
 

Many exceptions

 
In Python, one or more exceptions are used in the except.
 
Syntax
  1. try:  
  2.      //statement    
  3. except:  
  4.      //statement    
  5. except:  
  6.      //statement    
Program
  1. try:  
  2.     a=int (input ("enter value\t=")) #user input  
  3.     b=10/a  
  4.     print (b)  
  5. except ZeroDivisionError:  
  6.     print ("ZeroDivisionError")  
  7. except#other errors  
  8.     print ("something went wrong")  
Output
 
When a Zero Error occurs, it executes the “except ZeroDivisionError” block and another for other errors.
 
 

else

 
The statement itself says that if a given condition is true or false. True means executing the “try” block to the output. False means executing the “else” block to the output.
 
Syntax
  1. try:  
  2.      //statement    
  3. except:  
  4.      //statement    
  5. else:  
  6.      //statement    
Program
  1. try:#try and except both are error.  
  2.   x=3  
  3. except:  
  4.   print("x value is 3")  
  5. else:# try block does not raise any errors because the else block is executed.  
  6.   print("Something went wrong")  
Output
 
The try block does not raise any errors because the else block is executed.
 
 

Finally

 
The finally block is used to execute code, irrespective of the result of the try and except blocks. The finally block will be executed one time in the output screen.
 
Syntax
  1. try:  
  2.      //statement    
  3. except:  
  4.      //statement    
  5. finally:  
  6.      //statement    
Program
  1. a=10  
  2. try:  
  3.   print(a)  
  4. except:  
  5.   print("Something went wrong")  
  6. finally:  
  7.   print("hello c# corner")  
Output
 
Try and finally blocks are executed in the output screen.
 
 

Raise

 
Raise exceptions in several ways by using the raise statement.
 
Syntax 
  1. raise (Exception(args(traceback)))    
Program
  1. a = 1000#a value is above 100  
  2. if 100 < a:  
  3.   raise Exception("Sorry, the numbers above 100")    
Output
 
Raise is checking if a condition occurs or not.
 
 

Conclusion

 
In this article, we have seen types of exceptions in Python. I hope this article was useful to you. Thanks for reading!


Similar Articles