Error Unions
- Error sets. An error set names failures: error{OutOfMemory, NotFound}: errors as types, not strings.
- try. try propagates an error return early: the one-word question mark of Zig.
- catch. catch handles or defaults: value = parse(s) catch 0: recovery inline and visible.
Optionals
- Optionals. A ?T holds a value or null: absence in the type, unwrapped explicitly.
- orelse. orelse supplies the fallback: name orelse "guest": absence answered inline.
- if unwrapping. if (maybe) |value| unwraps into a capture: the branch owns a real value.
Explicit Failure Handling
- errdefer. errdefer runs cleanup only on error paths: partially-built resources unwound exactly.
- Error return traces. Debug builds carry error traces: the path an error travelled, printed on failure.
- No exceptions ever. With no exceptions, every failure path is visible in the code that takes it.
Before and after this level
- Before: Level 4: Structs, Enums & Unions: Structs, Enums, Tagged Unions.
- Next: Level 6: Comptime: Compile-Time Evaluation, Generic Programming, Compile-Time Code Generation.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Zig Level 5: Errors & Optionals cover?
Error Unions, Optionals, Explicit Failure Handling: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 4: Structs, Enums & Unions, which covers Structs, Enums, Tagged Unions.