捕捉
Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.
Starts a catch block.
Aliases: catch
Syntax: <structured-statement> cape <name> <block>
Category#
errors
Related#
Examples#
radix/corpus/cape/cape.fab (canonical · keyword)#
Starts a catch block.
# =============================================================================
# cape — Starts a catch block.
# =============================================================================
#
# What this teaches:
# • Error recovery — `fac { … } cape err { … }` attempts a block and catches
# errors with a named handler
# • Guarded execution — code inside the `fac` block can `iace` errors that
# are intercepted by the `cape` handler
#
# Common mistakes:
# • attaching `cape` to a bare `{ }` block — `cape` only attaches to `fac` blocks and structured statements; use `fac { ... } cape err { ... }`
#
# See also: fac, iace, ⇥
# =============================================================================
# cape — catch handler on fac
#
# GRAMMAR:
# facStmt :← 'fac' block 'cape' ident block
#
# EXPECTED OUTPUT:
# Scalar stdout smoke (see body).
#
# BACKEND:
# Rust/Go: fac/cape emit gap (whitelist: cape/cape.fab).
#
main {
var int attempt ← 0
do {
attempt ← attempt + 1
if attempt > 1 {
throw "simulated failure"
}
print "attempt §"(attempt)
}
catch err {
print "caught"
}
}Expected output:
attempt 1
radix/corpus/cape/receptio-decline.fab (supporting · reject)#
cape AIR decline rows: whole-loop dum/itera cape stays out under the loop-spectrum causes, a rethrow from a recovery arm declines (rethrow_in_recovery), and the cura construct keeps its independent deferred-init keep-out.
# =============================================================================
# receptio decline — the Tier 0.5 keep-outs stay fail-closed.
# =============================================================================
#
# Decline rows (reject reason in each comment):
# 1. whole-loop cape on `dum` — one trailing cape wraps the whole loop
# (never re-armed per iteration), and the loop spectrum stays an AIR
# keep-out: SEM059 dum_loop. A loop statement has no normal-exit
# value, so the recovery arm also fails the outer-type rule:
# SEM059 recovery_type_mismatch
# 2. whole-loop cape on `itera` — same per-loop attachment, same
# keep-out: SEM059 itera_loop plus recovery_type_mismatch
# 3. rethrow — a throw from inside the catch arm is a rethrow; it
# declines to Tier 2 fail-closed and is never silently treated as a
# recovered value: SEM059 rethrow_in_recovery
# 4. cura construct — the cape handler itself does not decline; the
# construct's independent deferred-init ruling keeps the function out
# of AIR: SEM059 deferred_local_init
#
# Common mistakes:
# • expecting the trailing loop `cape` to recover per iteration — the
# handler is per-loop by the grammar; per-iteration recovery would
# need new syntax and its own goal
# • expecting a rethrow to admit because the region admitted — the
# recovery arm is checked as a catch region; throwing from it is the
# named Tier 2 decline
# • reading the cura row as a handler decline — the handler admits; the
# construct's own keep-out is what fails
#
# See also: cape, dum, itera, cura, iace, receptio-air
# =============================================================================
# 1. whole-loop cape on dum — loop-spectrum keep-out
@ radix lane "air"
functio custos_dum(bivalens pergo) → vacuum {
dum pergo {
iace "x"
} cape err { }
}
# 2. whole-loop cape on itera — loop-spectrum keep-out
@ radix lane "air"
functio custos_itera() → vacuum {
itera ab 0‥4 fixum i {
iace "x"
} cape err { }
}
# 3. rethrow — a throw inside the catch arm declines to Tier 2
@ radix lane "air"
functio reicit(bivalens malum) → numerus ⇥ textus {
fac {
si malum { iace "x" }
redde 1
} cape err {
iace "reicitur"
}
redde 2
}
# 4. cura construct — the handler admits; deferred-init keeps the function out
@ radix lane "air"
functio cura_recepta() → numerus {
cura "arena" fixum _ res { redde 1 } cape err { redde 0 }
redde 3
}Expected: compilation rejects this example.