AI engineering glossary · Python core
What is finally?
finally: A block attached to try that always runs afterwards, whether or not an exception occurred, even when the block returns. It is used for cleanup such as closing files or releasing connections.
finally: an example
try: f = open('a.txt') ... finally: f.close() makes sure the file is closed even if reading fails.
Related terms
More Python core terms
Where you learn it
finally is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.