Skip to content

Zig Error: switch must handle all possibilities

error: switch must handle all possibilities means your switch has no prong for some value the expression could have. Zig’s switch must be exhaustive: every possible value has to lead somewhere. For an enum or an error set, a note names exactly what’s missing. For integers, there’s no note, because the missing values would be most of the number range, and the fix is an else prong. For the other errors in this series, see Zig Errors Explained.

const std = @import("std");
const Color = enum { red, green, blue };
pub fn main() void {
const c: Color = .green;
switch (c) {
.red => std.debug.print("stop\n", .{}),
.green => std.debug.print("go\n", .{}),
}
}
main.zig:7:5: error: switch must handle all possibilities
switch (c) {
^~~~~~
main.zig:3:34: note: unhandled enumeration value: 'blue'
const Color = enum { red, green, blue };
^~~~
main.zig:3:15: note: enum 'main.Color' declared here
const Color = enum { red, green, blue };
^~~~~~~~~~~~~~~~~~~~~~~~~

The first note is the useful one: unhandled enumeration value: 'blue', with a caret on blue in the enum. If several values are missing, you get one note per value.

A switch in Zig is an expression that must always produce a result or run some prong. If a value could slip through with no prong, the program wouldn’t know what to do, so Zig refuses to build it. This is one of Zig’s most useful checks: when you add a new value to an enum later, every switch that forgot about it shows up as an error. In C, a missing case just does nothing; in Zig, you find out while compiling. The reference’s Exhaustive Switching section has the rule and a short example that fails with this same message. Zig Switch covers the basics.

For enums, list the missing value. This is almost always the best fix:

const std = @import("std");
const Color = enum { red, green, blue };
pub fn main() void {
const c: Color = .green;
switch (c) {
.red => std.debug.print("stop\n", .{}),
.green => std.debug.print("go\n", .{}),
.blue => std.debug.print("calm\n", .{}),
}
}

Output:

go

If several values do the same thing, put them in one prong with commas: .green, .blue => ....

An integer has far too many values to list. A u8 alone has 256:

const std = @import("std");
pub fn main() void {
var n: u8 = 2;
_ = &n;
const word = switch (n) {
1 => "one",
2 => "two",
3 => "three",
};
std.debug.print("{s}\n", .{word});
}
main.zig:6:18: error: switch must handle all possibilities
const word = switch (n) {
^~~~~~

No note this time. Add an else prong for “everything else”:

const std = @import("std");
pub fn main() void {
var n: u8 = 2;
_ = &n;
const word = switch (n) {
1 => "one",
2 => "two",
3 => "three",
else => "many",
};
std.debug.print("{s}\n", .{word});
}

Output:

two

Ranges help too: 4...9 => "a few" covers 4 to 9 (three dots, both ends included). Strings can’t be switched on at all; use if with std.mem.eql, or turn the string into an enum first with std.meta.stringToEnum.

switch on an error works like an enum, and the note names the error you missed:

const std = @import("std");
pub fn main() void {
const r = std.fmt.parseInt(u8, "300", 10);
if (r) |v| {
std.debug.print("{d}\n", .{v});
} else |err| switch (err) {
error.InvalidCharacter => std.debug.print("not a number\n", .{}),
}
}
main.zig:7:18: error: switch must handle all possibilities
} else |err| switch (err) {
^~~~~~
main.zig:7:18: note: unhandled error value: 'error.Overflow'

parseInt can also fail because the number doesn’t fit in a u8. Handle that case too:

const std = @import("std");
pub fn main() void {
const r = std.fmt.parseInt(u8, "300", 10);
if (r) |v| {
std.debug.print("{d}\n", .{v});
} else |err| switch (err) {
error.InvalidCharacter => std.debug.print("not a number\n", .{}),
error.Overflow => std.debug.print("too big for a u8\n", .{}),
}
}

Output:

too big for a u8

This is a big part of why Zig errors are easy to handle well: the compiler knows every error a function can return and makes sure you’ve thought about each one. See Zig Errors.

If you list every value and add else, Zig complains about that too:

main.zig:11:14: error: unreachable else prong; all cases already handled
else => std.debug.print("other\n", .{}),
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The else can never run, so remove it.

  • After adding a value to an enum or a variant to a tagged union. Every switch on it lights up.
  • Switching on numbers like a menu choice or a status code, without else.
  • Handling errors from a function with switch (err), especially standard library functions with several errors.
  • Tagged unions, which work like enums: list every variant. See Zig Tagged Unions.

Add the missing state to the enum’s switch so the program builds and prints wait. Don’t use else.

const std = @import("std");
const Light = enum { red, yellow, green, off };
fn action(l: Light) []const u8 {
return switch (l) {
.red => "stop",
.yellow => "wait",
.green => "go",
};
}
pub fn main() void {
std.debug.print("{s}\n", .{action(.yellow)});
}

Hint: the note will name the missing value. What should a driver do at a light that’s off?

Show solution
const std = @import("std");
const Light = enum { red, yellow, green, off };
fn action(l: Light) []const u8 {
return switch (l) {
.red => "stop",
.yellow => "wait",
.green => "go",
.off => "look both ways",
};
}
pub fn main() void {
std.debug.print("{s}\n", .{action(.yellow)});
}

Output:

wait

Listing .off keeps the switch exhaustive. If you later add .flashing to Light, Zig will point you straight at this function.

  • Every switch must be exhaustive: each possible value needs a prong.
  • For enums and error sets, the note names each missing value. Add a prong for it.
  • For integers, add else. Ranges like 4...9 help.
  • Prefer listing enum values over else, so new values get caught later.
  • An else when everything is listed gives unreachable else prong: remove it.

Next up: unable to resolve comptime value, the error for a runtime value where Zig needs one known at compile time.

Quick check

1. Why is listing every enum value better than else in most cases?
2. A switch on a u32 has prongs for 0, 1 and 2. What fixes the error?
3. What does note: unhandled error value: 'error.Overflow' tell you?