throw
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Throws a recoverable error.
Syntax: throw <expression>
Category#
errors
Related#
Examples#
radix/corpus/iace/iace-si-guard.fab (canonical · keyword)#
Conditional throw with if guard.
# =============================================================================
# throw — Conditional throw with if guard
# =============================================================================
#
# What this teaches:
# • Conditional throw with if guard.
# • Related keywords: catch, panic, if, ⇥
#
# Common mistakes:
# • Using `throw if` without a `⇥` on the enclosing function or a `do/cape` wrapper — the guard sugar expands to `if cond { throw val }`, which still needs an error context.
#
# See also: catch, panic, if, ⇥
# =============================================================================
# throw with optional if guard
#
# throw <value> [if <condicio>]
#
# GRAMMAR:
# throwStmt :← 'throw' expr ['if' expr]
#
# The optional `if` guard is parser sugar:
# throw val if cond → if cond { throw val }
#
# Without the guard, throw is unconditional (unchanged).
#
# EXPECTED OUTPUT:
# "ok: valid input"
# "caught: empty input"
# "caught: negative"
fn validata(string input) → string ⇥ string {
# Guard: throw if input is empty
throw "empty input" if input.length() ≡ 0
return "valid: §"(input)
}
main {
# Happy path — guard not triggered
do {
const string a ← validata("hello")
print "ok: §"(a)
}
catch err {
print "unexpected: §"(err)
}
# Empty input — throw if guard triggers
do {
const string b ← validata("")
print "should not reach"
}
catch err {
print "caught: §"(err)
}
# throw with if on same line
do {
throw "force throw" if true
print "should not reach"
}
catch err {
print "caught: §"(err)
}
}radix/corpus/iace/iace.fab (canonical · keyword)#
Throws a recoverable error.
# =============================================================================
# throw — Throws a recoverable error
# =============================================================================
#
# What this teaches:
# • Throws a recoverable error.
# • Related keywords: ⇥, catch, panic, return
#
# Common mistakes:
# • Using `throw` without a `⇥` on the enclosing function or a `do { ... } catch err { ... }` wrapper — `throw` requires an error-handling context (SEM010).
#
# See also: ⇥, catch, panic, return
# =============================================================================
# throw — recoverable throw and catch handler
#
# throw <value>
# do { <body> }
# catch err { <handler> }
#
# GRAMMAR:
# throwStmt :← 'throw' expr
# facStmt :← 'do' block catchClause?
#
# EXPECTED OUTPUT:
# Caught error messages from do/cape recovery paths.
#
# BACKEND:
# Rust/Go lowering does not emit throw/cape yet — compile-only smoke.
main {
# Bare throw with string payload
do {
throw "Something went wrong"
}
catch err {
print "Caught:", err
}
# throw with interpolated message
const int code ← 404
do {
throw "Error code: §"(code)
}
catch err {
print "Caught:", err
}
# Conditional throw inside do — validation guard
const int value ← -5
do {
if value ≺ 0 {
throw "Value must be non-negative"
}
print "Value is valid"
}
catch err {
print "Validation failed:", err
}
}Expected output:
Caught: Something went wrong
Caught: Error code: 404
Validation failed: Value must be non-negative