bắt
Translation status: Tiếng Việt reader-locale proof. Term names and code fences follow the vi pack; supporting prose may still be English.
Starts a catch block.
Aliases: catch
Syntax: <structured-statement> bắt <name> <block>
Category#
errors
Related#
Examples#
radix/corpus/cape/cape.fab (canonical · keyword)#
Starts a catch block.
# =============================================================================
# bắt — Starts a catch block.
# =============================================================================
#
# What this teaches:
# • Error recovery — `làm { … } bắt err { … }` attempts a block and catches
# errors with a named handler
# • Guarded execution — code inside the `làm` block can `ném` errors that
# are intercepted by the `bắt` handler
#
# Common mistakes:
# • attaching `bắt` to a bare `{ }` block — `bắt` only attaches to `làm` blocks and structured statements; use `làm { ... } bắt err { ... }`
#
# See also: làm, ném, ⇥
# =============================================================================
# bắt — catch handler on làm
#
# GRAMMAR:
# facStmt :← 'làm' block 'bắt' ident block
#
# EXPECTED OUTPUT:
# Scalar stdout smoke (see body).
#
# BACKEND:
# Rust/Go: làm/cape emit gap (whitelist: bắt/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)#
bắt AIR decline rows: whole-loop trong_khi/itera bắt 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 bắt on `trong_khi` — one trailing bắt 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 bắt on `lặp` — 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 `bắt` 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: bắt, trong_khi, lặp, ném, receptio-air
# =============================================================================
# 1. whole-loop bắt on trong_khi — loop-spectrum keep-out
@ radix làn "air"
hàm custos_dum(logic pergo) → trống {
trong_khi pergo {
ném "x"
} bắt err { }
}
# 2. whole-loop bắt on lặp — loop-spectrum keep-out
@ radix làn "air"
hàm custos_itera() → trống {
lặp khoảng 0‥4 hằng i {
ném "x"
} bắt err { }
}
# 3. rethrow — a throw inside the catch arm declines to Tier 2
@ radix làn "air"
hàm reicit(logic malum) → số ⇥ văn_bản {
làm {
nếu malum { ném "x" }
trả 1
} bắt err {
ném "reicitur"
}
trả 2
}Expected: compilation rejects this example.