Skip to main content

core/Runtime

Runtime utilities. These functions were originally part of the Debug module.

import Runtime "mo:core/Runtime";

Function trap

func trap(errorMessage : Text) : None

trap(t) traps execution with a user-provided diagnostic message.

The caller of a future whose execution called trap(t) will observe the trap as an Error value, thrown at await, with code #canister_error and message m. Here m is a more descriptive Text message derived from the provided t. See example for more details.

NOTE: Other execution environments that cannot handle traps may only propagate the trap and terminate execution, with or without some descriptive message.

Runtime.trap("An error occurred!");

Function unreachable

func unreachable() : None

unreachable() traps execution when code that should be unreachable is reached.

This function is useful for marking code paths that should never be executed, such as after exhaustive pattern matches or unreachable control flow branches. If execution reaches this function, it indicates a programming error.

let number = switch (?5) {
case (?n) n;
case null Runtime.unreachable();
};
assert number == 5;