Kết xuấtvi

ném

Translation status: Tiếng Việt reader-locale proof. Term names and code fences follow the vi pack; supporting prose may still be English.

Throws a recoverable error.

Syntax: ném <expression>

Category#

errors

Examples#

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

Conditional throw with nếu guard.

# =============================================================================
# ném — Conditional throw with nếu guard
# =============================================================================
#
# What this teaches:
#   • Conditional throw with nếu guard.
#   • Related keywords: bắt, chết, nếu, ⇥
#
# Common mistakes:
#   • Using `ném nếu` without a `⇥` on the enclosing function or a `làm/cape` wrapper — the guard sugar expands to `nếu cond { ném val }`, which still needs an error context.
#
# See also: bắt, chết, nếu, ⇥
# =============================================================================

# ném with optional nếu guard
#
# ném <value> [nếu <condicio>]
#
# GRAMMAR:
#   throwStmt :← 'ném' expr ['nếu' expr]
#
# The optional `nếu` guard is parser sugar:
#   ném val nếu cond  →  nếu cond { ném val }
#
# Without the guard, ném 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 — ném nếu guard triggers
    do {
        const string b ← validata("")
        print "should not reach"
    }
    catch err {
        print "caught: §"(err)
    }

    # ném with nếu 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.

# =============================================================================
# ném — Throws a recoverable error
# =============================================================================
#
# What this teaches:
#   • Throws a recoverable error.
#   • Related keywords: ⇥, bắt, chết, trả
#
# Common mistakes:
#   • Using `ném` without a `⇥` on the enclosing function or a `làm { ... } bắt err { ... }` wrapper — `ném` requires an error-handling context (SEM010).
#
# See also: ⇥, bắt, chết, trả
# =============================================================================

# ném — recoverable throw and bắt handler
#
# ném <value>
# làm { <body> }
# bắt err { <handler> }
#
# GRAMMAR:
#   throwStmt :← 'ném' expr
#   facStmt   :← 'làm' block catchClause?
#
# EXPECTED OUTPUT:
#   Caught error messages from làm/cape recovery paths.
#
# BACKEND:
#   Rust/Go lowering does not emit ném/cape yet — compile-only smoke.

main {
    # Bare ném with văn_bản payload
    do {
        throw "Something went wrong"
    }
    catch err {
        print "Caught:", err
    }

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

    # Conditional ném inside làm — 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