0Pricing
Mojo Academy · Lesson

Catching with try/except

Recover from a failure gracefully.

Catching with try/except is a free Mojo Academy lesson on CoddyKit — lesson 2 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.

Catching the Fall

Raising signals a problem, but someone must catch it. Mojo uses a try block to run risky code and recover if it fails. 🪤

The try Block

Code that might fail goes inside try. If it raises, Mojo jumps straight to the matching recovery code below.

try:
    risky_step()

The except Block

The except block holds your recovery plan. It only runs when the try block actually raised an error.

try:
    risky_step()
except:
    print("recovered")

Reading the Error

You can name the caught error to read its message. Here e holds the details of what went wrong.

try:
    risky_step()
except e:
    print("failed:", e)

Normal Path First

If nothing raises, the try block finishes and the except block is skipped entirely. The happy path stays clean.

Recovering Gracefully

Catching lets you show a friendly message or use a fallback value instead of letting the whole program crash. ✨

Keep try Blocks Small

Wrap only the risky line in try, not your whole function. Small blocks make it clear what could fail.

Catching Calls That Raise

Calling a function that may raise requires a try. Here we safely attempt a withdrawal and handle a rejection.

try:
    withdraw(-5)
except e:
    print("denied:", e)

Don't Swallow Silently

An empty except that does nothing hides bugs. At minimum log the error so problems stay visible.

Catch Where You Can Help

Only catch an error where you can actually do something useful about it. Otherwise let it propagate upward.

Try Plus Except as a Pair

Together try and except turn a possible failure into a controlled outcome your program can continue past.

Quick Check

Recall when each block of a try/except actually runs.

Recap

You learned to wrap risky code in try, recover in except, read the error with a name, and keep blocks small and meaningful. 🎯

Frequently asked questions

Is the “Catching with try/except” lesson free?

Yes — the full text of “Catching with try/except” 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 “Catching with try/except”?

Recover from a failure gracefully. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Catching with try/except” 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

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