0PricingLogin
Zig Academy · Lesson

Recover with catch

Provide a fallback when an error occurs.

Errors Need a Plan

A function can return an error union like !u32. Before you use the result you must decide what happens on the failure path.

Meet catch

The catch operator turns an error union into a plain value by giving a fallback to use whenever the call returns an error.

const n = parse(text) catch 0;

All lessons in this course

  1. Recover with catch
  2. Switch on Specific Errors
  3. errdefer for Failure Cleanup
  4. Wrapping and Re-throwing Errors
← Back to Zig Academy