08 · Error Handling
Local Result boundaries
attempt evaluates a block and wraps its normal tail in Ok. A failing Result question mark exits the nearest attempt as Err, so the caller can continue. A Result returned as the tail remains nested. Panics are not caught; optional propagation needs an explicit error conversion.
On this page
attempt evaluates a block and wraps its normal tail in Ok. A failing Result question mark exits the nearest attempt as Err, so the caller can continue. A Result returned as the tail remains nested. Panics are not caught; optional propagation needs an explicit error conversion.
Complete example
Local Result boundaries.
11-attempt.zolo
// Feature: local Result boundaries
// Syntax: attempt { let value = fallible()?; value }
// When to use: handle a Result failure locally and continue the caller.
use std::Result
fn read_number(text: str) -> Result<int, str> {
if text == "42" { return Result.Ok(42) }
return Result.Err("bad number")
}
fn main() {
let good: Result<int, str> = attempt {
let value = read_number("42")?
value + 1
}
match good {
Result::Ok(value) => print(value),
Result::Err(error) => print(error),
}
// expected: 43
let bad: Result<int, str> = attempt {
let value = read_number("other")?
value + 1
}
match bad {
Result::Ok(value) => print(value),
Result::Err(error) => print(error),
}
// expected: bad number
print("caller continues")
// expected: caller continues
}
main()
Rules and behavior
Conditions and error recovery · Documentation
From a checkout of the repository:
zolo examples/features/08-error-handling/11-attempt.zolo
See also