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")
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 PagesThe try
keyword.
The except
keyword.
Track your progress - it's free!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4