渲染zh-Hans

执行

Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans pack; supporting prose may still be English.

Starts a scoped do-while style loop.

Aliases: do

Syntax: 执行 <block> [捕获 <name> <block>] [当 <condition>]

Category#

control-flow

Examples#

radix/corpus/fac/fac-cape.fab (canonical · keyword)#

Starts a scoped do-while style loop.

# =============================================================================
# 执行 — Starts a scoped do-while style loop
# =============================================================================
#
# What this teaches:
#   • Starts a scoped do-while style loop.
#   • Related keywords: 当, 捕获, 闭包, 继续
#
# Common mistakes:
#   • Using `捕获` on a non-`执行` block — `捕获` only attaches to `执行` statements, not arbitrary blocks.
#
# See also: 当, 捕获, 闭包, 继续
# =============================================================================

# 执行 — try block with 捕获 error handler
#
# 执行 { <body> }
# 捕获 err { <handler> }
#
# GRAMMAR:
#   facStmt :← '执行' block '捕获' ident block
#
# EXPECTED OUTPUT:
#   none — success path then simulated failure on attempt 2
#
# BACKEND:
#   Rust/Go: 执行/cape not yet emitted — compile-only smoke.

main {
    do {
        print "Block executed successfully"
    }
    catch err {
        print "Caught error: §"(err.nuntius)
    }

    # 抛错 on second attempt; 捕获 recovers and prints err.nuntius
    var int attempts ← 0
    do {
        attempts ← attempts + 1
        print "Attempt §"(attempts)
        if attempts ≡ 2 {
            throw "Simulated failure"
        }
    }
    catch err {
        print "Failed on attempt §: §"(attempts, err.nuntius)
    }
}