0Pricing
Zig Academy · Lesson

Returning Errors from Functions

Make failure explicit in signatures.

Failure in the Signature

When a function can fail, say so in its return type. An error union like ParseError!u32 warns every caller up front.

fn parse(s: []const u8) ParseError!u32 {
    // ...
}

Return the Success Value

On the happy path you just return the payload as normal. Zig wraps that plain value into the success side of the union for you.

return 42;

All lessons in this course

  1. Error Sets and the !T Union
  2. Returning Errors from Functions
  3. Propagate Failures with try
  4. Inferred Error Sets
← Back to Zig Academy