finally
noun
say it: “FY-nuh-lee” /ˈfaɪnəli/
In Python, the block after try and except that runs no matter what happened, which makes it the place for clean-up.
What does finally do?
finally runs after try and except no matter what happened, which makes it the place for clean-up.
try:
print("Charging card...")
raise ConnectionError("network down")
except ConnectionError:
print("Payment failed")
finally:
print("Closing payment screen") # always runs
In a sentence
- “The finally block closes the payment screen whether the charge worked or not.”
- “Code in finally runs even when the except block raises again.”
Level: Journeyman · More Python terms at this level
Updated