捕获
Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans pack; supporting prose may still be English.
Starts a catch block.
Aliases: catch
Syntax: <structured-statement> 捕获 <name> <block>
Category#
errors
Related#
Examples#
radix/corpus/cape/cape.fab (canonical · keyword)#
Starts a catch block.
# =============================================================================
# 捕获 — Starts a catch block.
# =============================================================================
#
# What this teaches:
# • Error recovery — `执行 { … } 捕获 err { … }` attempts a block and catches
# errors with a named handler
# • Guarded execution — code inside the `执行` block can `抛错` errors that
# are intercepted by the `捕获` handler
#
# Common mistakes:
# • attaching `捕获` to a bare `{ }` block — `捕获` only attaches to `执行` blocks and structured statements; use `执行 { ... } 捕获 err { ... }`
#
# See also: 执行, 抛错, ⇥
# =============================================================================
# 捕获 — catch handler on 执行
#
# GRAMMAR:
# facStmt :← '执行' block '捕获' ident block
#
# EXPECTED OUTPUT:
# Scalar stdout smoke (see body).
#
# BACKEND:
# Rust/Go: 执行/cape emit gap (whitelist: 捕获/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)#
捕获 AIR decline rows: whole-loop 当/itera 捕获 stays out under the loop-spectrum causes, a rethrow from a recovery arm declines (rethrow_in_recovery).
# =============================================================================
# receptio decline — the Tier 0.5 keep-outs stay fail-closed.
# =============================================================================
#
# Decline rows (reject reason in each comment):
# 1. whole-loop 捕获 on `当` — one trailing 捕获 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 捕获 on `遍历` — 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
#
# Common mistakes:
# • expecting the trailing loop `捕获` 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
#
# See also: 捕获, 当, 遍历, 抛错, receptio-air
# =============================================================================
# 1. whole-loop 捕获 on 当 — loop-spectrum keep-out
@ radix 车道 "air"
函数 custos_dum(布尔 pergo) → 无值 {
当 pergo {
抛错 "x"
} 捕获 err { }
}
# 2. whole-loop 捕获 on 遍历 — loop-spectrum keep-out
@ radix 车道 "air"
函数 custos_itera() → 无值 {
遍历 范围 0‥4 常量 i {
抛错 "x"
} 捕获 err { }
}
# 3. rethrow — a throw inside the catch arm declines to Tier 2
@ radix 车道 "air"
函数 reicit(布尔 malum) → 整数 ⇥ 文本 {
执行 {
如果 malum { 抛错 "x" }
返回 1
} 捕获 err {
抛错 "reicitur"
}
返回 2
}Expected: compilation rejects this example.