Unrecoverable Errors with panic!

In this chapter, we'll explore stop execution when something goes wrong beyond recovery.

Unrecoverable Errors with panic!

When bad things happen that your code can't recover from, Mana has the panic macro. When it executes, your program prints an error message, cleans up, and quits.

Using panic!

module main
 
fn main() -> void {
    panic("crash and burn")
}

Output:

thread 'main' panicked at 'crash and burn', src/main.mana:2:5

Panic from Library Code

Panic can also result from bugs in your code. For example:

module main
 
fn main() -> void {
    let v = [1, 2, 3]
    v[99]  // Panic: index out of bounds
}

This is a programming error—attempting to access an invalid index. Mana panics rather than returning invalid memory.

When to Use panic!

Use panic! when:

  • A bug is detected (invariant violation)
  • There's no way to recover
  • Continuing would be worse than stopping

For expected errors (like file not found), use Result instead.

Continue to Recoverable Errors with Result.