0Pricing
Mojo Academy · Lesson

Raising an Error

Signal that something went wrong.

Raising an Error is a free Mojo Academy lesson on CoddyKit — lesson 1 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.

When Things Go Wrong

Sometimes a function hits a situation it cannot handle, like a missing file or bad input. Mojo lets you signal that with an error. ⚠️

The raise Keyword

To signal a failure, you use the raise keyword. It stops the current work and reports that something went wrong.

raise Error("something went wrong")

Errors Carry a Message

When you raise, you pass a short message describing the problem. Whoever handles the error can read that text.

raise Error("file not found")

Raising Inside a Function

A function can raise when its input is invalid. Here a negative amount is rejected before any further work happens.

fn withdraw(amount: Int) raises:
    if amount < 0:
        raise Error("amount must be positive")

Raising Stops Execution

Once raise runs, the rest of the function is skipped. Control jumps out, looking for code that can deal with the error.

Why Not Just Return -1?

Returning a magic value like -1 is easy to ignore. An error cannot be silently skipped, making failures far harder to miss.

Errors Travel Up

An unhandled error keeps moving up through the callers until something catches it. This is called propagation.

Guarding Your Inputs

Raising early on bad input is a clean habit. Check the values first, then raise before the real logic ever runs.

if name == "":
    raise Error("name is required")

One Clear Reason

Give each raise a clear, specific message. Good wording turns a crash into a helpful clue for whoever debugs it.

Errors Are Not Crashes

A raised error is a structured signal, not a random crash. Your program can recover from it instead of just dying.

Raise When You Can't Continue

The rule of thumb: raise only when a function truly cannot finish its job correctly. Don't raise for normal outcomes.

Quick Check

Think about what the raise keyword actually does in Mojo.

Recap

You learned to signal failures with raise, attach a clear message, and let errors stop bad work instead of returning magic values. 🎯

Frequently asked questions

Is the “Raising an Error” lesson free?

Yes — the full text of “Raising an Error” 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 “Raising an Error”?

Signal that something went wrong. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Raising an Error” 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