In this blog, we will see how to handle exceptions in Python application. If you are new to Python, please read my previous blog – Introduction to Python Programming
This blog will help you to understand try, except and finally block in Python code. Exception handling matters a lot to create a stable robust application in Python.
In any programming language, handling errors or exceptions is very important. If you write good code, then the code must have the capabilities to handle the errors.
Python Exception Handling : Types of Error
Let us take a look at the types of error in an application before reading the exception handling in Python in details.
Compile Time Error – If there is a syntax error or spelling error in any of the keyword, you will get compile time error.
Run time Error – If the application throws error while processing output, it is called as Run time error.
For eg – Division by zero is the most common run time error example.
Logical Error – In case user doesn’t get desired output, it is a logical error. It means either there is an error in calculation or the logic implemented to return output.
How to handle Exception in Python?
Now we will see how can we handle exception in Python code by an example.
Let’s write below code and execute this –
i = 10 j = 1 print(i/j) print("Next statement")
i = 10 j = 0 print(i/j) print("Next statement")
Syntax to handle exception in Python –
Instead of try, catch, finally like other programming language, we have try, except and finally in Python to handle exceptions errors.
What is try-except in Python?
try: #Write Logic Here except Exception: #Write message to show when exception occurred
try –
except –
Examples of Exception Handling in Python
i = 10 j = 0 try: print(i/j) except Exception: print("There is some error.") print("Next statement")
i = 10 j = 0 try: print(i/j) except Exception as ex: print(ex) print("Next statement")
Finally block in Python –
Example of Exception Handling in Python with Finally
try: print('Database Connection Open') print('Code executed') except Exception as ex: print('There is some error', ex) finally: print('Close Database Connection')
i = 10 j = 0 try: print(i/j) print('Code executed') except ZeroDivisionError as ex1: print(ex1) except FileNotFoundError as ex2: print(ex2) except ArithmeticError as ex3: print(ex3) except Exception as ex: print(ex) finally: print('Execution Completed')
Handle Multiple Exceptions in single Except Block –
try: a = int(input('Enter a number')) b = int(input('Enter another number')) print(a/b) except (ZeroDivisionError,ValueError) as ex: print('Exception:-', ex.__class__.__name__) print('Exception Details:-', ex) print('Please enter valid input') finally: print('Execution Completed')
Exception Handling in Python – Summary
In general, Python exception handling is similar to other programming language.
In Python, we have try-except-finally block to handle exceptions.
A single except can handle multiple type of Exceptions.
Previous Blog – Introduction to Python Programming
Watch Video on – Exception Handling in Python
You may like other blogs –
Interview Questions and Answers Series –