finally keyword in Python

Prerequisites: Exception Handling, try and except in Python

In programming, there may be some situation in which the current method ends up while handling some exceptions. But the method may require some additional steps before its termination, like closing a file or a network and so on.
So, in order to handle these situations, Python provides a keyword finally, which is always executed after try and except blocks. The finally block always executes after normal termination of try block or after try block terminates due to some exception.

Syntax:

try:
       # Some Code.... 

except:
       # optional block
       # Handling of exception (if required)

finally:
      # Some code .....(always executed)      

Important Points –

  • finally block is always executed after leaving the try statement. In case if some exception was not handled by except block, it is re-raised after execution of finally block.
  • finally block is used to deallocate the system resources.
  • One can use finally just after try without using except block, but no exception is handled in that case.

Example #1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate finally
  
# No exception Exception raised in try block
try:
    k = 5//0 # raises divide by zero exception.
    print(k)
  
# handles zerodivision exception    
except ZeroDivisionError:   
    print("Can't divide by zero")
      
finally:
    # this block is always executed 
    # regardless of exception generation.
    print('This is always executed'

chevron_right


Output:

Can't divide by zero
This is always executed

 
Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate finally
  
try:
    k = 5//1 # No exception raised
    print(k)
  
# intends to handle zerodivision exception    
except ZeroDivisionError:   
    print("Can't divide by zero")
      
finally:
    # this block is always executed 
    # regardless of exception generation.
    print('This is always executed'

chevron_right


Output:

5
This is always executed

 
Example #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate finally
  
# Exception is not handled
try:
    k = 5//0 # exception raised
    print(k)
      
finally:
    # this block is always executed 
    # regardless of exception generation.
    print('This is always executed'

chevron_right


Output:

This is always executed

Runtime Error –

Unhandled Exception 
    k=5//0 #No exception raised
ZeroDivisionError: integer division or modulo by zero

Explanation:
In above code, the exception is generated integer division or modulo by zero, which was not handled. The exception was re-raised after execution of finally block. This shows that finally block is executed regardless of exception is handled or not.



My Personal Notes arrow_drop_up

Love to write, Competitive programming is fun, Python is way

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.