08 · Error Handling
Fallible function bodies
A function returning T fails E exposes Result<T, E> to its caller. Return successful values directly; fail error exits with Err. The ? operator propagates failure through the nearest Result boundary, including a local attempt block.
On this page
A function returning T fails E exposes Result<T, E> to its caller. Return successful values directly; fail error exits with Err. The ? operator propagates failure through the nearest Result boundary, including a local attempt block.
Complete example
Fallible function bodies.
fallible_invoice.zolo
// An invoice rejects missing stock and exposes Result<int, str> to its caller.
fn subtotal(quantity: int, unit_price: int) -> int fails str {
if quantity <= 0 { fail "quantity must be positive" }
if unit_price < 0 { fail "price cannot be negative" }
quantity * unit_price
}
fn invoice(quantity: int, unit_price: int) -> int fails str {
let total = subtotal(quantity, unit_price)?
total + 12
}
match invoice(3, 25) {
.Ok(total) => print("Invoice: {total}"),
.Err(message) => print("Cannot create invoice: {message}"),
}
Reference
zolo run examples/features/fallible_invoice.zolo --no-cache
See also