0Pricing
Zig Academy · Lesson

defer vs errdefer in Practice

Choose always-run versus on-error.

defer vs errdefer in Practice 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.

Two Cleanup Keywords

Zig gives you two scheduling tools: defer always runs, while errdefer runs only when the scope exits with an error.

defer Means Always

Use defer when cleanup must happen no matter what, like closing a file you opened for the whole function.

const f = try open();
defer f.close();

errdefer Means On Failure

An errdefer only fires if the scope leaves through an error. On a successful return, it is quietly skipped.

const buf = try alloc();
errdefer free(buf);

Why Skip on Success

Sometimes you build a value to return it. On success the caller owns it, so you must not free it; errdefer respects that.

Half-Built Objects

errdefer shines in constructors. If a later step fails, it undoes the earlier allocations so you never leak a half-built object.

const a = try alloc();
errdefer free(a);
const b = try alloc();

Success Hands Off Ownership

When every step succeeds, the function returns the value and the errdefers do not run. Ownership transfers cleanly to the caller.

return MyType{ .a = a, .b = b };

They Combine Nicely

You can mix both. A defer handles temporary scratch memory, while an errdefer protects the result you plan to return.

Both Honor Reverse Order

Like defer, multiple errdefers unwind in reverse. On error, Zig runs the matching defers and errdefers together, newest first.

Placement Matters

Put each errdefer right after the step it protects. It only guards failures that happen after its own line is reached.

A Simple Rule

Ask one question: should this cleanup run on success too? Yes means defer; only-on-error means errdefer.

Leak-Free by Construction

Together these keywords let you write functions that are leak-free on every path, success or failure, without manual bookkeeping.

Quick Check

When does errdefer run compared to defer?

Recap

Reach for defer when cleanup must always run, and errdefer when it should only undo work on the error path. ✅

Frequently asked questions

Is the “defer vs errdefer in Practice” lesson free?

Yes — the full text of “defer vs errdefer in Practice” 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 “defer vs errdefer in Practice”?

Choose always-run versus on-error. 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 “defer vs errdefer in Practice” 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

  1. What defer Guarantees
  2. Execution Order of Multiple defers
  3. defer vs errdefer in Practice
  4. Pairing Open with Close
← Back to Zig Academy