0Pricing
Mojo Academy · Lesson

The raises Annotation

Mark functions that can fail.

The raises Annotation is a free Mojo Academy lesson on CoddyKit — lesson 3 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.

Honest About Failure

Mojo wants functions to be honest about whether they can fail. The raises keyword marks a function that might raise an error. 📣

Adding raises

Put raises right after the argument list. It tells callers this function may not always succeed.

fn parse(text: String) raises -> Int:
    ...

def Can Always Raise

A flexible def function may raise without any annotation. Mojo treats def as able to fail by default.

def load(name: String):
    raise Error("missing")

fn Must Declare It

A strict fn function cannot raise unless you add raises. This makes failure explicit and easy to spot.

fn safe(x: Int) -> Int:
    return x + 1

Calling a raising Function

Calling something marked raises means you must handle the error, usually inside a try block.

try:
    var n = parse("42")
except e:
    print(e)

Errors Bubble Through raises

If a raising function is called inside another raises function, the error simply propagates up without a try.

Non-raising Means Safe

A function without raises promises it will not raise. Callers can use it freely without try blocks.

The Compiler Checks You

Mojo's compiler rejects a raise inside a non-raising fn. This catches mismatches before your program ever runs.

Self-Documenting Code

The raises keyword acts as living documentation. One glance at the signature tells you if a call can fail.

Keep the Surface Small

Mark only the functions that truly can fail with raises. Fewer raising functions means simpler, safer call sites.

raises and Return Types

You combine raises with a normal return type. The function returns a value on success or raises on failure.

fn divide(a: Int, b: Int) raises -> Int:
    if b == 0:
        raise Error("divide by zero")
    return a // b

Quick Check

Think about how fn and the raises keyword work together.

Recap

You learned that raises declares a function may fail, that def can always raise while fn must opt in, and the compiler enforces it. 🎯

Frequently asked questions

Is the “The raises Annotation” lesson free?

Yes — the full text of “The raises Annotation” 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 “The raises Annotation”?

Mark functions that can fail. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The raises Annotation” 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