จับ
Translation status: ภาษาไทย reader-locale proof. Term names and code fences follow the th-TH 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.