渲染zh-Hans

崩溃

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

Raises a fatal error or panic.

Syntax: 崩溃 <expression>

Category#

errors

Examples#

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

Conditional fatal abort with 如果 guard.

# =============================================================================
# 崩溃 — Conditional fatal abort with 如果 guard.
# =============================================================================
#
# What this teaches:
#   • fatal abort with `崩溃` — unconditional and conditional (`如果` guard) forms
#   • invariant enforcement — using `崩溃` with guards to validate function preconditions
#   • the `如果` guard is parser sugar for `如果 cond { 崩溃 "msg" }`
#
# Common mistakes:
#   • Using 崩溃 for recoverable errors — use 抛错 with a 如果 guard and ⇥ for recoverable failure paths instead.
#
# See also: 抛错, 则, 如果
# =============================================================================

# 崩溃 with optional 如果 guard
#
# 崩溃 <nuntius> [如果 <condicio>]
#
# GRAMMAR:
#   abortStmt :← '崩溃' expr ['如果' expr]
#
# The optional `如果` guard is parser sugar:
#   崩溃 "msg" 如果 cond  →  如果 cond { 崩溃 "msg" }
#
# Without the guard, 崩溃 is unconditional (unchanged).
#
# EXPECTED OUTPUT:
#   "within range"
#   "positive"
#   "not empty"

fn clamp(int x, int min, int max) → int {
    # Guard: abort if min > max — invariant violation
    panic "range inverted" if min ≻ max

    # Guard: abort if value out of range
    panic "value out of range" if x ≺ min or x ≻ max

    return x
}

fn accessus(list<int> res, int index) → int {
    # Unconditional 崩溃 — unchanged form
    if index ≺ 0 or index ≥ res.length() {
        panic "index out of bounds"
    }
    return res[index]
}

main {
    # Happy path — guards not triggered
    const int n ← clamp(5, 0, 10)
    print "within range"

    # Happy path — empty list guard not triggered
    const list<int> xs ← [1, 2, 3]
    const int y ← accessus(xs, 1)
    print "positive"

    const list<int> ys ← [42]
    const int z ← accessus(ys, 0)
    print "not empty"
}

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

Raises a fatal error or panic.

# =============================================================================
# 崩溃 — Raises a fatal error or panic.
# =============================================================================
#
# What this teaches:
#   • fatal unrecoverable abort with `崩溃` — halts execution with an error message
#   • invariant checking — using `崩溃` for divisor and bounds checks
#
# Common mistakes:
#   • Using 崩溃 for recoverable errors — use 抛错 with ⇥ for recoverable failure paths instead.
#
# See also: 抛错, 则
# =============================================================================

# 崩溃 — fatal unrecoverable abort
#
# 崩溃 <nuntius>
#
# GRAMMAR:
#   abortStmt :← '崩溃' expr
#
# EXPECTED OUTPUT:
#   Happy-path division and 列表 access; 崩溃 paths are commented out.

fn divide(int a, int b) → float {
    # Use 崩溃 for invariant violations
    if b ≡ 0 {
        panic "divisio per nihilum"
    }
    return (a ↦ float) / (b ↦ float)
}

fn accipe(list<int> numeri, int index) → int {
    # Bounds check with 崩溃
    if index ≺ 0 or index ≥ numeri.length() {
        panic "index extra fines"
    }
    return numeri[index]
}

main {
    # Happy path — 崩溃 branches not taken
    const float value ← divide(10, 2)
    # 5.0
    print "valor: §"(value)

    # This would call 崩溃:
    # 常量 _ malum ← divide(10, 0)

    # Lista accessus
    const list<int> nums ← [1, 2, 3]
    const int lectum ← accipe(nums, 1)
    # 2
    print "valor: §"(lectum)
}