Python finally Keyword

Used with try/catch blocks and marks a block of code that will be executed no matter if an exception was thrown or not. The finally keyword is used in conjunction with the try keyword and except keywords. Regardless of the exception, the code in the finally block will always run.

Example

Python
def divide(n, d):
    try:
        result = n / d
    except:
        print("Oops, dividing by 0!")
        result = float('inf')
    finally:
        print('This will always be printed: Result = {result}')

print('6 / 2:')
divide(6, 2)
print('10 / 0:')
divide(10, 0)

Output

6 / 2:
Result = 3.0
10 / 0:
Oops, dividing by 0!
This will always be printed: Result = inf

Notice how the code in the finally block is always run.