渲染zh-Hant

拋出

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant 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