Propagate Failures with try
Bubble errors up with one keyword.
Propagate Failures with try is a free Zig Academy lesson on CoddyKit — lesson 3 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.
Handling Gets Repetitive
Writing an if-else around every fallible call to forward its error gets noisy fast. Zig offers a shortcut: the try keyword.
try Unwraps or Returns
Put try before a fallible call. On success it hands you the value; on error it returns that error from your current function.
const n = try parse(input);It Is Pure Sugar
That one line is exactly the same as catching the error and returning it yourself. try just writes the boilerplate for you.
const n = parse(input) catch |e| return e;Your Function Must Be Fallible
Because try can return an error, the enclosing function needs an error union return type. Otherwise the compiler rejects it.
fn run() !void {
const n = try parse(input);
}Chain Many tries
Stack several try calls in a row. Each one stops the function early on failure, so the happy path reads top to bottom.
const f = try open(path);
const data = try read(f);try in the Middle of Expressions
You can place try right inside a larger expression. The unwrapped value flows straight into the surrounding call or math.
const total = base + try parse(input);try with void Calls
Even when a call returns !void, use try to forward any failure. You get no value back, only the early return on error.
try file.writeAll(data);Errors Climb the Stack
With try in every layer, a failure deep down propagates all the way up until some caller finally decides to handle it.
Readable Happy Path
The big win is clarity: error forwarding fades into a single keyword, so the main logic stays front and center.
try Does Not Handle
Remember that try only forwards an error; it never recovers from one. To react locally you reach for catch instead.
When to Forward
Use try when the right move is to let the caller deal with failure. Most intermediate functions simply pass errors upward.
Quick Check
You write try doThing() inside a function. What happens if doThing returns an error?
Recap
You learned that try unwraps on success and returns the error otherwise. It is shorthand for catch and forward, keeping code clean. ✅
Frequently asked questions
Is the “Propagate Failures with try” lesson free?
Yes — the full text of “Propagate Failures with try” 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 “Propagate Failures with try”?
Bubble errors up with one keyword. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Propagate Failures with try” 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.
All lessons in this course
- Error Sets and the !T Union
- Returning Errors from Functions
- Propagate Failures with try
- Inferred Error Sets