0Pricing
Mojo Academy · Lesson

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

  1. Raising an Error
  2. Catching with try/except
  3. The raises Annotation
  4. Cleanup with finally
← Back to Mojo Academy