The finally
keyword is used in conjunction with the try
and except
keywords. Whatever the issue of the try / except
blocks, the finally
block will be executed (even if there is an explicit request for function output).
Example Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#!/usr/bin/python3 # -*- coding: utf-8 -*- from random import random def demo(): try: name = int( random()*3 ) value = 33 / denom print( "Result == " + str( value ) ) return except: print( "Intercepted and corrected problem" ) finally: print( "Bye bye" ) print( "End of the function" ) # Launching the function # In all cases, the finally block is executed demo() |
Output: