渲染zh-Hans

抛错

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

Throws a recoverable error.

Syntax: 抛错 <expression>

Category#

errors

Examples#

radix/corpus/iace/iace-si-guard.fab (canonical · keyword)#

Conditional throw with 如果 guard.

# =============================================================================
# 抛错 — Conditional throw with 如果 guard
# =============================================================================
#
# What this teaches:
#   • Conditional throw with 如果 guard.
#   • Related keywords: 捕获, 崩溃, 如果, ⇥
#
# Common mistakes:
#   • Using `抛错 如果` without a `⇥` on the enclosing function or a `执行/cape` wrapper — the guard sugar expands to `如果 cond { 抛错 val }`, which still needs an error context.
#
# See also: 捕获, 崩溃, 如果, ⇥
# =============================================================================

# 抛错 with optional 如果 guard
#
# 抛错 <value> [如果 <condicio>]
#
# GRAMMAR:
#   throwStmt :← '抛错' expr ['如果' expr]
#
# The optional `如果` guard is parser sugar:
#   抛错 val 如果 cond  →  如果 cond { 抛错 val }
#
# Without the guard, 抛错 is unconditional (unchanged).
#
# EXPECTED OUTPUT:
#   "ok: valid input"
#   "caught: empty input"
#   "caught: negative"

fn validata(string input) → string ⇥ string {
    # Guard: throw if input is empty
    throw "empty input" if input.length() ≡ 0

    return "valid: §"(input)
}

main {
    # Happy path — guard not triggered
    do {
        const string a ← validata("hello")
        print "ok: §"(a)
    }
    catch err {
        print "unexpected: §"(err)
    }

    # Empty input — 抛错 如果 guard triggers
    do {
        const string b ← validata("")
        print "should not reach"
    }
    catch err {
        print "caught: §"(err)
    }

    # 抛错 with 如果 on same line
    do {
        throw "force throw" if true
        print "should not reach"
    }
    catch err {
        print "caught: §"(err)
    }
}

radix/corpus/iace/iace.fab (canonical · keyword)#

Throws a recoverable error.

# =============================================================================
# 抛错 — Throws a recoverable error
# =============================================================================
#
# What this teaches:
#   • Throws a recoverable error.
#   • Related keywords: ⇥, 捕获, 崩溃, 返回
#
# Common mistakes:
#   • Using `抛错` without a `⇥` on the enclosing function or a `执行 { ... } 捕获 err { ... }` wrapper — `抛错` requires an error-handling context (SEM010).
#
# See also: ⇥, 捕获, 崩溃, 返回
# =============================================================================

# 抛错 — recoverable throw and 捕获 handler
#
# 抛错 <value>
# 执行 { <body> }
# 捕获 err { <handler> }
#
# GRAMMAR:
#   throwStmt :← '抛错' expr
#   facStmt   :← '执行' block catchClause?
#
# EXPECTED OUTPUT:
#   Caught error messages from 执行/cape recovery paths.
#
# BACKEND:
#   Rust/Go lowering does not emit 抛错/cape yet — compile-only smoke.

main {
    # Bare 抛错 with 文本 payload
    do {
        throw "Something went wrong"
    }
    catch err {
        print "Caught:", err
    }

    # 抛错 with interpolated message
    const int code ← 404
    do {
        throw "Error code: §"(code)
    }
    catch err {
        print "Caught:", err
    }

    # Conditional 抛错 inside 执行 — validation guard
    const int value ← -5
    do {
        if value ≺ 0 {
            throw "Value must be non-negative"
        }
        print "Value is valid"
    }
    catch err {
        print "Validation failed:", err
    }
}

Expected output:

Caught: Something went wrong
Caught: Error code: 404
Validation failed: Value must be non-negative