Skip to main content

Traps

A trap is a runtime error that causes execution to abort immediately. Common causes include division by zero, out-of-bounds array access, or pattern match failure.

If a trap occurs during message execution, only that message fails. Other messages continue execution.

To trigger a trap manually, use Debug.trap.

import Debug "mo:base/Debug";

Debug.trap("oops!");

Assertions

An assertion checks a condition at runtime and traps if it fails.

let n = 10;
assert n % 2 == 1; // Traps
let n = 10;
assert n % 2 == 0; // Succeeds

Assertions help catch logic errors early, but should not be used for regular error handling.

Logo