Python finally Keyword

❮ Python Keywords


Example

The finally block will always be executed, no matter if the try block raises an error or not:

try:
  x > 3
except:
  print("Something went wrong")
else:
  print("Nothing went wrong")
finally:
  print("The try...except block is finished")
Try it Yourself »

Definition and Usage

The finally keyword is used in try...except blocks. It defines a block of code to run when the try...except...else block is final.

The finally block will be executed no matter if the try block raises an error or not.

This can be useful to close objects and clean up resources.


Related Pages

The try keyword.

The except keyword.


❮ Python Keywords

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.