panic
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Raises a fatal error or panic.
Syntax: panic <expression>
Category#
errors
Related#
Examples#
radix/corpus/mori/mori-si-guard.fab (canonical · keyword)#
Conditional fatal abort with if guard.
# =============================================================================
# panic — Conditional fatal abort with if guard.
# =============================================================================
#
# What this teaches:
# • fatal abort with `panic` — unconditional and conditional (`if` guard) forms
# • invariant enforcement — using `panic` with guards to validate function preconditions
# • the `if` guard is parser sugar for `if cond { panic "msg" }`
#
# Common mistakes:
# • Using panic for recoverable errors — use throw with a if guard and ⇥ for recoverable failure paths instead.
#
# See also: throw, then, if
# =============================================================================
# panic with optional if guard
#
# panic <nuntius> [if <condicio>]
#
# GRAMMAR:
# abortStmt :← 'panic' expr ['if' expr]
#
# The optional `if` guard is parser sugar:
# panic "msg" if cond → if cond { panic "msg" }
#
# Without the guard, panic is unconditional (unchanged).
#
# EXPECTED OUTPUT:
# "within range"
# "positive"
# "not empty"
fn clamp(int x, int min, int max) → int {
# Guard: abort if min > max — invariant violation
panic "range inverted" if min ≻ max
# Guard: abort if value out of range
panic "value out of range" if x ≺ min or x ≻ max
return x
}
fn accessus(list<int> res, int index) → int {
# Unconditional panic — unchanged form
if index ≺ 0 or index ≥ res.length() {
panic "index out of bounds"
}
return res[index]
}
main {
# Happy path — guards not triggered
const int n ← clamp(5, 0, 10)
print "within range"
# Happy path — empty list guard not triggered
const list<int> xs ← [1, 2, 3]
const int y ← accessus(xs, 1)
print "positive"
const list<int> ys ← [42]
const int z ← accessus(ys, 0)
print "not empty"
}radix/corpus/mori/mori.fab (canonical · keyword)#
Raises a fatal error or panic.
# =============================================================================
# panic — Raises a fatal error or panic.
# =============================================================================
#
# What this teaches:
# • fatal unrecoverable abort with `panic` — halts execution with an error message
# • invariant checking — using `panic` for divisor and bounds checks
#
# Common mistakes:
# • Using panic for recoverable errors — use throw with ⇥ for recoverable failure paths instead.
#
# See also: throw, then
# =============================================================================
# panic — fatal unrecoverable abort
#
# panic <nuntius>
#
# GRAMMAR:
# abortStmt :← 'panic' expr
#
# EXPECTED OUTPUT:
# Happy-path division and list access; panic paths are commented out.
fn divide(int a, int b) → float {
# Use panic for invariant violations
if b ≡ 0 {
panic "divisio per nihilum"
}
return (a ↦ float) / (b ↦ float)
}
fn accipe(list<int> numeri, int index) → int {
# Bounds check with panic
if index ≺ 0 or index ≥ numeri.length() {
panic "index extra fines"
}
return numeri[index]
}
main {
# Happy path — panic branches not taken
const float value ← divide(10, 2)
# 5.0
print "valor: §"(value)
# This would call panic:
# const _ malum ← divide(10, 0)
# Lista accessus
const list<int> nums ← [1, 2, 3]
const int lectum ← accipe(nums, 1)
# 2
print "valor: §"(lectum)
}