Updated April 12, 2023
Introduction to Finally in Python
There can be times when the method that is currently running can end abruptly trying to handle some exceptions in programming but there will be a necessity for the method to complete few activities before it terminates like closing a network or closing an opened file and so on and to handle such situations a keyword is provided in python called finally which is a block of code executed after the execution of try block of code and except the block of code and this finally block of code is executed after the execution of try block of code normally or after the termination of try block of code due to some exception.
Syntax:
The syntax of the Finally keyword in Python is as follows:
try:
{
//Block of code
}
except:
{
//Block of code
// this can be optional
//Exception handling, if necessary
}
finally:
{
//Block of code which is always executed
}
Functions of Finally keyword in Python
- Whenever a current method terminates abruptly, there are chances that the method may have been using resources that are allocated to it and will not be freed or closed upon the termination of the current method.
- In such situations finally, a block of code is used to free the resources that were allocated in the try block of code and this finally block of code is always executed regardless of the exception being handled by the except block or not.
- If an exception is not handled by the except the block of code after the control leaves the try block of code, the exception is raised again after the completion of the execution of finally block of code.
- The system resources that needs to be deallocated is done by the finally block of code.
- Finally block of code can be used right after the try block of code without the except block of code but the exception that is going to be raised will not be handled.
Examples of Finally in Python
Following are examples are given below:
Example #1
Python program to demonstrate the use of Finally block of code in a program where the exception is handled by the except block of code:
Code:
#python program to demonstrate finally block
#try and except blocks to handle the exception
try:
#divide by zero exception will be raised
r = 10//0
print(r)
#divide by zero exception is handled here
except ZeroDivisionError:
print("The divisor cannot be zero")
#finally block of code which will always be executed regardless of the exception is handled or not
finally:
print('This is finally block of code which is always executed regardless of the exception is handled or not.')
Output:
Code Explanation: In the above program to demonstrate finally block, try and except blocks of code are used to handle the exception divide by zero. A variable r is used to store the result of the division of 10/0 which cannot be done as the divisor cannot be zero. Hence an exception is raised by the except block which says, the divisor cannot be zero, and then the finally block of code is executed which will always be executed regardless of the exception being handled or not. The output of the program is shown in the snapshot above.
Example #2
Python program to demonstrate the use of finally keyword in a program where the exception is handled by the except the block of code:
Code:
#python program to demonstrate finally block
#try and except blocks to handle the exception
try:
#no exception will be raised
r = 10//10
print(r)
#divide by zero exception is handled here in case if it happens
except ZeroDivisionError:
print("The divisor cannot be zero")
#finally block of code which will always be executed regardless of the exception is raised or not or if it is raised, regardless of it is handled or not
finally:
print('This is finally block of code which is always executed regardless of the exception is raised or not or if it is raised, regardless of it is handled or not.')
Output:
Code Explanation: In the above program to demonstrate finally block, try and except blocks of code are used to handle the exception divide by zero if it occurs. A variable r is used to store the result of the division of 10/10 which results in one without causing any exception. Hence the except block of code is not executed, which is the divisor cannot be zero and then the finally block of code is executed which will always be executed regardless of the exception is raised or not and if it is raised, regardless of the exception being handled or not. The output of the program is shown in the snapshot above.
Example #3
C# program to demonstrate the use of finally keyword in a program where there is no exception handling:
Code:
#python program to demonstrate finally block
#try and except blocks to handle the exception
try:
#divide by zero exception is expected
r = 10//0
print(r)
#finally block of code which will always be executed regardless of the exception is handled or not
finally:
print('This is finally block of code which is always executed regardless of the exception is raised or not or if it is raised, regardless of it is handled or not.')
Output:
Code Explanation: In the above program to demonstrate the finally block, try block of code is used in which a variable r is used to store the result of the division of 10/0 which is not possible and causes an exception. But we have not defined the except block of code to handle the exception. Hence there is no exception handling and then the finally block of code is executed which will always be executed regardless of whether the exception is raised or not and if it is raised, regardless of the exception being handled or not. The output of the program is shown in the snapshot above.
Recommended Articles
This is a guide to Finally in Python. Here we also discuss the introduction and working of finally keyword in python along with different examples and its code implementation. You may also have a look at the following articles to learn more –