Cleanup with finally
Run code no matter what happens.
Code That Always Runs
Some cleanup must happen whether things succeed or fail, like closing a file. Mojo's finally block guarantees that. 🧹
Adding finally
The finally block sits after try and except. Its code runs no matter how the try block ends.
try:
use_resource()
finally:
cleanup()All lessons in this course
- Raising an Error
- Catching with try/except
- The raises Annotation
- Cleanup with finally