Curriculum Errors
Errors
Zig errors are first-class values from a finite set declared per-API. error{ Empty, BadInput } declares a set; !T is the error-union return type. try is sugar for "propagate-on-error, unwrap-on-success." catch handles. There's no exception hierarchy, no try/catch ladder, no fall-through — every error case is named, every callsite is explicit. Builds on Module 3: errdefer finally makes sense in its native habitat.
4.1
Error sets
An error set is a finite enumeration of named errors — declared as error{ Empty, BadDigit, ... }. Different APIs declare their own sets; the compiler tracks which errors a function…
4.2
try and catch
There are three ways to consume an error union. try expr is the propagator: on error, return the error to the caller; on success, unwrap the value. expr catch <default> substitutes…
A function that can fail declares a return type with a leading bang: !T means "either a T or an error." The error set is usually INFERRED from the function body — every return erro…
4.4
Panics vs errors
Errors are for conditions the caller should be expected to handle (bad input, out of memory, missing file). Panics are for conditions that indicate a programmer bug — invariants th…