Recover with catch
Provide a fallback when an error occurs.
Recover with catch is a free Zig Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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;The Fallback Replaces the Error
If the call succeeds you get the real value. If it errors you get the right side of catch instead, so n is always a usable number.
const n = parse(text) catch -1;catch Is an Expression
Because catch yields a value, you can drop it inline anywhere a value is expected, like straight into a function argument.
print(parse(text) catch 0);Capture the Error
Add a pipe capture to inspect what went wrong. The name inside the bars holds the specific error value that was returned.
const n = parse(text) catch |err| {
log(err);
return 0;
};catch Can Run a Block
The right side of catch may be a whole block. Use it to log, compute a fallback, or break out of a loop before continuing.
const v = open(path) catch |e| blk: {
break :blk fallback;
};catch Can Return Early
You can return from inside catch to leave the function when recovery is impossible. The code after it never runs on error.
const f = open(path) catch return error.MissingFile;catch unreachable
Writing catch unreachable asserts the call cannot fail. In a safe build a real error here panics instead of being ignored.
const n = parse("42") catch unreachable;Use unreachable Carefully
Reach for catch unreachable only when an error is truly impossible. If you are unsure, supply a real fallback instead.
catch vs try
Use catch when you can handle the error here and now. Use try when you would rather pass the error up to the caller.
Recovery Is Explicit
Every error union forces a choice, so a failure can never be silently dropped. With catch you decide exactly how to recover.
Quick Check
You want to keep going with a safe default when a call fails. Which expression fits?
Recap
You used catch to recover from errors with a fallback, a block, an early return, or an assertion. Failure is always handled on purpose. ✅
Frequently asked questions
Is the “Recover with catch” lesson free?
Yes — the full text of “Recover with catch” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Recover with catch”?
Provide a fallback when an error occurs. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Recover with catch” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.