XU XUv0.16.0

05 · Errors

Do not use Result / Ok / Err / ?. >> expr throws errors.Error; catch with >>> (err) { }.

Source

@main

#errors

fn read_name(ok bool) string {
    if !ok {
        >> errors.new("empty name")
    }
    << "ada"
}

fn load_or_fallback(ok bool) string {
    << read_name(ok) >>> (err) {
        println(err.msg())
        << "fallback"
    }
}

fn main() int {
    s := load_or_fallback(false)
    println(s)

    t := read_name(true) >>> (err) {
        println(err.msg())
        << 1
    }
    println(t)
    << 0
}

Build:

xu run examples/05_errors/main.xu
# or: xu build examples/05_errors/main.xu
# default artifact is ./main on mac/linux, ./main.exe on Windows

Run from the separate examples repository root; source files live under examples/.

Migrating from legacy or syntax (0.15.0)

Xu 0.15.0 removed legacy or error handling (or (err) { } and or >>), which now fails with a hard parser error directing you to >>> (err) { } and >>>, while >> expr throws errors. or is now a normal identifier (boolean OR remains ||), and xu-lsp offers a quick-fix code action to convert legacy or handlers to >>>; see VS Code / Cursor LSP code actions.