0Pricing
Zig Academy · Lesson

Error Sets and the !T Union

Errors as first-class values.

Error Sets and the !T Union 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 Are Values

In Zig there are no exceptions. An error is just an ordinary value your function can return, so failure is visible in the code.

Declare an Error Set

An error set is a named group of possible errors. You list the variants much like an enum of things that can go wrong.

const FileError = error{
    NotFound,
    PermissionDenied,
};

Refer to One Error

Each member of an error set is reached with a dot. The value FileError.NotFound is a single concrete error you can return.

return FileError.NotFound;

Anonymous Error Sets

You do not always need a name. Writing error{Empty} inline creates a tiny one-off error set right where it is used.

return error.Empty;

The Bang Means Maybe-Error

The ! in a type builds an error union. The type before it is the success value; an error may appear in its place instead.

FileError!u32

Read !T Out Loud

You can read !u32 as either a u32 or an error. The single value carries both outcomes, so callers must pick one.

Two Sides of the Union

An error union has exactly two shapes at runtime: a valid payload of type T, or one of the errors from the set. Never both.

Set Before the Bang

Put the error set on the left of ! to say exactly which errors are allowed. This documents failure right in the type.

fn parse(s: []const u8) FileError!u32 {
    // ...
}

Merge Error Sets

The || operator on two error sets combines them into one. This lets a function admit errors from several sources at once.

const AllError = FileError || NetError;

anyerror Is the Superset

The built-in anyerror contains every possible error. It is convenient but loses the precision of a named set, so use it sparingly.

fn risky() anyerror!void {}

Why Sets Help

Because errors live in the type, the compiler knows every failure a function can produce. Nothing slips past you unhandled.

Quick Check

You see the type FileError!u32 on a function. What does it describe?

Recap

You met error sets, the !T union, set merging with ||, and anyerror. Errors are plain values the type system tracks for you. ✅

Frequently asked questions

Is the “Error Sets and the !T Union” lesson free?

Yes — the full text of “Error Sets and the !T Union” 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 “Error Sets and the !T Union”?

Errors as first-class values. 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 “Error Sets and the !T Union” 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. Error Sets and the !T Union
  2. Returning Errors from Functions
  3. Propagate Failures with try
  4. Inferred Error Sets
← Back to Zig Academy