Cleanup with finally
Run code no matter what happens.
Cleanup with finally is a free Mojo Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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()Runs After Success
If the try block finishes cleanly, finally still runs right afterward. It is your guaranteed last step.
Runs After an Error
If the try block raises, finally runs before the error continues upward. Cleanup never gets skipped.
Perfect for Resources
Use finally to release things you opened: files, connections, or locks. They close even when something fails.
try:
var f = open("data.txt", "r")
read_all(f)
finally:
f.close()try, except, finally Together
You can combine all three: try the work, except to recover, and finally to clean up in every case.
try:
work()
except e:
print(e)
finally:
cleanup()finally Without except
You can pair finally with just try, no except. The error still propagates, but cleanup runs first.
Order of Events
The flow is simple: run try, then except if it raised, and finally always last. Predictable every time.
Avoid Heavy Work Here
Keep finally focused on cleanup. Avoid risky logic inside it, since an error there can hide the original problem.
No Leaks, No Surprises
With finally, resources are released reliably. Your program avoids leaks even on the worst, most error-prone paths.
A Safety Net for Cleanup
Think of finally as a safety net under your risky code, catching the cleanup duty no matter what happens above. 🪢
Quick Check
Recall exactly when a finally block runs.
Recap
You learned that finally always runs after try, success or failure, making it the reliable place to release files and resources. 🎯
Frequently asked questions
Is the “Cleanup with finally” lesson free?
Yes — the full text of “Cleanup with finally” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Cleanup with finally”?
Run code no matter what happens. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Cleanup with finally” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Raising an Error
- Catching with try/except
- The raises Annotation
- Cleanup with finally