typeover
curriculum

Curriculum Errors

module 4 of 4

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.


themes

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…

9 ready begin →

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…

9 ready begin →

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…

9 ready begin →

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…

9 ready begin →