0Pricing
Zig Academy · Lesson

Switch on Specific Errors

Branch by which error was returned.

Switch on Specific Errors is a free Zig Academy lesson on CoddyKit — lesson 2 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.

Not All Errors Are Equal

One fallback rarely fits every failure. A missing file and bad input often need different handling, so you want to branch by which error occurred.

Capture Then switch

Capture the error from catch, then feed it into a switch. Each case can respond to one specific error in its own way.

open(path) catch |err| switch (err) {
    error.NotFound => create(path),
    else => return err,
};

Errors Are switch Values

An error like error.NotFound is a normal value, so switch matches it just like it matches an integer or an enum tag.

switch (err) {
    error.Timeout => retry(),
    error.Denied => reportDenied(),
}

switch Must Be Exhaustive

A switch on an error must cover every possible error in the set. Zig refuses to compile if even one case is missing.

Cover the Rest with else

Add an else branch to catch any error you did not name. This keeps the switch exhaustive without listing every variant.

switch (err) {
    error.NotFound => createDefault(),
    else => return err,
}

Each Arm Returns a Value

Because switch is an expression, every arm can produce a value. The whole catch then yields whatever the matching arm gives back.

const n = parse(s) catch |e| switch (e) {
    error.Empty => 0,
    else => -1,
};

Group Errors with Commas

List several errors in one arm separated by commas when they share a response. This keeps related cases tidy and together.

switch (err) {
    error.Timeout, error.Reset => retry(),
    else => return err,
}

Re-throw What You Cannot Handle

In an arm you can return the error to pass it upward. Handle the cases you understand and let the caller deal with the rest.

else => return err,

Pair with try First

Often you try the common path and only switch on the errors that truly need custom logic, keeping the happy path clean.

Clarity Through Specificity

Switching on errors documents exactly which failures you expect. The reader sees each case and how your code reacts to it.

Compiler Has Your Back

Because the switch must be complete, adding a new error later forces you to handle it. Missed cases become build errors, not bugs.

Quick Check

You switch on a captured error but list only some variants. What does Zig require?

Recap

You captured an error and used switch to branch per error, group related cases, and re-throw the rest. The compiler keeps it complete. ✅

Frequently asked questions

Is the “Switch on Specific Errors” lesson free?

Yes — the full text of “Switch on Specific Errors” 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 “Switch on Specific Errors”?

Branch by which error was returned. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Switch on Specific Errors” 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. Recover with catch
  2. Switch on Specific Errors
  3. errdefer for Failure Cleanup
  4. Wrapping and Re-throwing Errors
← Back to Zig Academy