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#
error
Related#
Examples#
radix/corpus/reference/keyword/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
if input.length() ≡ 0 {
throw "empty input"
}
return "valid: §"(input)
}
main {
# Happy path — guard not triggered
do {
let string a ← validata("hello")
print "ok: §"(a)
}
catch err {
print "unexpected: §"(err)
}
# Empty input — throw if guard triggers
do {
let string b ← validata("")
print "should not reach"
}
catch err {
print "caught: §"(err)
}
# throw with if on same line
do {
if true {
throw "force throw"
}
print "should not reach"
}
catch err {
print "caught: §"(err)
}
}Expected output:
ok: valid: hello
caught: empty input
caught: force throw
radix/corpus/reference/keyword/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
let int code ← 404
do {
throw "Error code: §"(code)
}
catch err {
print "Caught:", err
}
# Conditional throw inside do — validation guard
let 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
radix/corpus/reference/reject/iace/await-context-reject.fab (supporting · reject)#
An await binder needs an async context; a synchronous body has no suspension point.
# =============================================================================
# throw — an await binder outside an async context is a check error
# =============================================================================
# An await binder (`await_const` / `await_const`) suspends the callable that
# contains it. A synchronous body has nowhere to suspend, so the binding is
# refused before any code is emitted.
fn work() async → int {
return 1
}
fn sync() → int {
await_const int v ← work()
return v
}
main {
print sync()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/iace/default-requires-failable-reject.fab (supporting · reject)#
A ⊥ default follows a failable call or a conversion; anything else is rejected.
# =============================================================================
# throw — a ⊥ default with no failable call or conversion before it
# =============================================================================
# `⊥ v` supplies the recovery value of a failable call (`f() ⊥ v`) or a
# conversion (`x ↦ T ⊥ v`). After any other expression there is nothing to
# recover, so the leftover `⊥` is rejected as a parse error.
main {
let int x ← 5
print x ⊥ 0
}Expected: compilation rejects this example.
radix/corpus/reference/reject/iace/failable-no-handler-reject.fab (supporting · reject)#
A call to a ⇥ function with no handler is rejected: do/catch, trap, a ⊥ default, or a ⇥ on the caller.
# failable call — the caller must answer the ⇥ channel
#
# f is failable (→ int ⇥ string). h is not, and it neither wraps the call in
# do/catch, captures it, defaults it, nor declares its own ⇥ — so the checker
# rejects the file (SEM011 failable_call_requires_handler).
fn f(int n) → int ⇥ string {
if n ≡ 0 then throw "zero"
return n
}
fn h() → int {
return f(1)
}
main {
print h()
}Expected: compilation rejects this example.
radix/corpus/reference/reject/iace/iace-no-channel-reject.fab (supporting · reject)#
A throw with no error channel is rejected: give the function a ⇥ channel or wrap the throw in do/catch.
# throw — a throw needs an alternate exit
#
# h has no ⇥ error channel and no do/catch handler, so the throw has nowhere
# to go: the checker rejects the file (SEM010 iace_requires_alternate_exit).
fn h() → int {
throw "x"
}
main {
print h()
}Expected: compilation rejects this example.