渲染zh-Hant

捕捉

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> 捕捉 <name> <block>

Category#

errors

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.