Back to: Python Tutorials For Beginners and Professionals
Finally Block in Python with Examples
In this article, I am going to discuss Finally Block in Python with Examples. Please read our previous article where we discussed Exception Handling in Python with Examples. As part of this article, we are going to discuss the following pointers in detail.
- Why do we need Finally Block?
- Finally Block in Python
- Why not ‘try except’ block for clean-up activities?
- Different control flow cases of try except finally in python
Why do we need Finally Block?
In any project after completing the tasks, it is recommended to destroy all the unwanted things that are created for the completion of the task. For example, if I have opened a database connection in my code and done some tasks related to the database. After the tasks are completed, it is recommended to close the connection. These clean-up activities are very important and should be handled in our code. finally is a keyword in python that can be used to write a finally block to do clean-up activities.
Why not ‘try except’ block for clean-up activities?
Try block: There is no guarantee like every statement will be executed inside the try block. If an exception is raised at the second line of code in the try block, then the remaining lines of code in that block will not execute. So, it is not recommended to write clean-up code inside the try block.
Except block: There is no guarantee that an except block will execute. If there is no exception then except block won’t be executed. Hence, it is also not recommended to write clean-up code inside except block
Conclusion:
So, a separate block is required to write clean-up activities. This block of code should always execute irrespective of the exceptions. If no exception, then the clean-up code should execute. If an exception is raised and is also handled, then also clean-up code should execute. If an exception is raised, but not handled, then also clean-up code should execute.
All the above requirements, which can’t be achieved by try-except block, can be achieved by finally block. The finally block will be executed always, irrespective of the exception raised or not, exception handled or not.
Case 1: If there is no exception, then finally block will execute
Program: demo20.py
try: print("try block") except: print("except block") finally: print("finally block")
Output:
Case 2: If an exception is raised and handled with except block, then also finally block will be executed
Program: demo21.py
try: print("try block") print(10/0) except ZeroDivisionError: print("except block") finally: print("finally block")
Output:
Case 3: If an exception is raised inside the try block but except block doesn’t handle that particular exception, then also finally block will be executed.
Program: demo22.py
try: print("try block") print(10/0) except NameError: print("except block") finally: print("finally block")
Output:
If an exception is raised but is not handled, then the program is supposed to terminate abnormally. But before terminating, the finally block will be executed.
Different control flow cases of try except finally in python:
Case 1:
If there is no exception, then try, and finally blocks will execute, and except block won’t execute, leading to normal termination.
Program: demo23.py
print("One") print("Two") try: print("try block") except ZeroDivisionError: print("except block: Handling code") finally: print("finally block: clean-up activities") print("Four")
Output:
Case 2:
If an exception is raised inside the try block and the except block is handling that corresponding exception then try, except, and finally blocks will execute, leading to normal termination.
Program: demo24.py
print("One") print("Two") try: print("try block") print(10/0) except ZeroDivisionError: print("except block: Handling code") finally: print("finally block: clean-up activities") print("Four")
Output:
Case 3:
If an exception is raised inside the try block and the except block is not handling that corresponding exception then try and finally blocks will execute, leading to abnormal termination.
Program: demo25.py
print("One") print("Two") try: print("try block") print(10/0) except NameError: print("except block: Handling code") finally: print("finally block: clean-up activities") print("Four")
Output:
In the above program, since it is abnormal termination, the print statement after (print(“Four”)) the finally block didn’t get executed.
Case 4:
If an exception is raised inside the try block, then flow goes to the except block. And in the except block, assume, the particular exception is handled. Instead of handling exceptions, if the block itself raises another exception then the program will terminate abnormally. In this case also, before the program termination finally block will be executed.
Program: demo26.py
print("One") print("Two") try: print("try block") print(10/0) except ZeroDivisionError: print(10/0) finally: print("finally block: clean-up activities") print("Four")
Output:
Case 5:
If an exception is raised inside the finally block, then it’s always abnormal termination.
Program: demo27.py
print("One") print("Two") try: print("try block") print(10/0) except ZeroDivisionError: print("except block: Handling code") finally: print(10/0) print("Four")
Output:
In the next article, I am going to discuss Nested try-except-finally blocks in Python with Examples. Here, in this article, I try to explain Finally Block in Python with Examples. I hope you enjoy this Finally Block in Python with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.