渲染zh-Hans

拒绝

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

Reject a condition or throw a recoverable error.

Aliases: reject

Syntax: 拒绝 <expression> 抛错 <expression>

Category#

errors

Examples#

radix/corpus/meta/reice.fab (canonical · keyword)#

Reject a condition or throw a recoverable error.

# =============================================================================
# 拒绝 — Reject a condition or throw a recoverable error.
# =============================================================================
#
# What this teaches:
#   • Reject guard — `reject <condition> throw <error>` throws into the
#     function's ⇥ channel when the condition is true (en surface of `拒绝`)
#   • Boolean opposite of `require` — `require not (p) throw e` becomes
#     `reject p throw e`; no hand-rolled negation
#   • Typed payload — the true-path expression is a `variant`, the same shape
#     used at constructor guard ladders
#
# Common mistakes:
#   • Using `reject` in a function without `⇥ E` — SEM010 `iace_requires_alternate_exit`, same as `throw`
#   • Reading `reject` as `require` — it fails when the condition HOLDS
#
# See also: 需求, 断言, 抛错, 捕获, ⇥
# =============================================================================

# 拒绝 — recoverable reject statement
#
# reject <condition> throw <error>
#
# GRAMMAR:
#   reiceStmt :← '拒绝' expr '抛错' expr
#
# EXPECTED OUTPUT:
#   empty — success-path main only (reice.expected)

union GuardError {
    Invalid { string causa }
}

fn guarded(int value) → int ⇥ GuardError {
    reject value ≺ 0 throw variant Invalid { causa = "value must not be negative" }
    return value
}

main {
    do {
        const int ok ← guarded(1)
        assert ok ≡ 1
    }
    catch err {
        assert false panic "success path must not throw"
    }
}

test "true path is recoverable" {
    do {
        const int dropped ← guarded(-1)
        assert false panic "true path must throw"
    }
    catch err {
        # recovered — reject entered ⇥ and catch intercepted it
    }
}