Zig Error: error union of type 'X' is ignored
error: error union of type '...' is ignored means you called a function that can fail and did nothing with the result. In Zig, errors are values, and the compiler won’t let you drop one on the floor. The note under the error lists your three options: try, catch or if. You’ll hit this often at first, especially with list.append(...) and file functions. Older Zig versions worded it error is ignored; it’s the same rule. For the other errors in this series, see Zig Errors Explained.
The error message
Section titled “The error message”const std = @import("std");
pub fn main(init: std.process.Init) void { var list: std.ArrayList(i32) = .empty; defer list.deinit(init.gpa); list.append(init.gpa, 1);}main.zig:6:16: error: error union of type 'error{OutOfMemory}!void' is ignored list.append(init.gpa, 1); ~~~~~~~~~~~^~~~~~~~~~~~~main.zig:6:16: note: consider using 'try', 'catch', or 'if'error{OutOfMemory}!void reads as “either the error OutOfMemory, or nothing (success)”. append may need more memory, and asking for memory can fail.
The scary version
Section titled “The scary version”With your own functions, the type in the message can look much worse:
const std = @import("std");
fn save(n: i32) !void { if (n < 0) return error.Negative;}
pub fn main() void { save(5); std.debug.print("saved\n", .{});}main.zig:8:9: error: error union of type '@typeInfo(@typeInfo(@TypeOf(main.save)).@"fn".return_type.?).error_union.error_set!void' is ignored save(5); ~~~~^~~main.zig:8:9: note: consider using 'try', 'catch', or 'if'Don’t let it put you off. save returns !void without naming its errors, so Zig works out the error set itself and can only describe it with that long formula. The meaning is the same: this call can fail, and you ignored that. Only the part after the last ! matters for you: void.
What it means
Section titled “What it means”A function with ! in its return type returns an error union: either an error or a normal value. Zig doesn’t have exceptions that fly up the stack on their own. Instead, every call that can fail hands you a value that might be an error, and you have to say what happens if it is. That’s the whole idea of Zig’s error handling: no failure goes unnoticed. The Errors section of the language reference walks through error sets, try and catch. Zig Errors introduces it from the start.
How to fix it
Section titled “How to fix it”Fix 1: Pass the error up with try
Section titled “Fix 1: Pass the error up with try”In most code, the right move is to let the caller decide. try does that: if the call fails, the current function returns the same error. The function now can fail too, so its return type gets a !:
const std = @import("std");
pub fn main(init: std.process.Init) !void { var list: std.ArrayList(i32) = .empty; defer list.deinit(init.gpa); try list.append(init.gpa, 1); try list.append(init.gpa, 2); std.debug.print("{any}\n", .{list.items});}Output:
{ 1, 2 }Changing void to !void is the part people forget. If main returns an error, Zig prints the error’s name and an error trace, and the program exits with a failure code. See Zig Try and Catch.
Fix 2: Handle it with catch
Section titled “Fix 2: Handle it with catch”When this function is the right place to deal with the failure, use catch. The |err| capture gives you the error so you can report it:
const std = @import("std");
fn save(n: i32) !void { if (n < 0) return error.Negative; std.debug.print("saved {d}\n", .{n});}
pub fn main() void { save(-1) catch |err| { std.debug.print("could not save: {t}\n", .{err}); }; save(5) catch {};}Output:
could not save: Negativesaved 5{t} prints the error’s name. catch {} on the second call says “if this fails, do nothing”. That’s allowed, but only write it when ignoring the failure is really fine, and a short comment saying why helps the next reader. For a function that returns a value, catch gives a fallback instead: parse(text) catch 0.
Fix 3: Branch with if
Section titled “Fix 3: Branch with if”if with an error union runs one block on success and another on failure:
const std = @import("std");
fn half(n: i32) !i32 { if (@rem(n, 2) != 0) return error.Odd; return @divExact(n, 2);}
pub fn main() void { if (half(7)) |h| { std.debug.print("half is {d}\n", .{h}); } else |err| { std.debug.print("can't halve 7: {t}\n", .{err}); }}Output:
can't halve 7: OddThis reads well when both paths do real work.
error union is discarded
Section titled “error union is discarded”Writing _ = save(5); doesn’t get you out of it. Discarding the value with _ is allowed for normal values, but not for error unions:
const std = @import("std");
fn save(n: i32) !void { if (n < 0) return error.Negative;}
pub fn main() void { _ = save(5); std.debug.print("saved\n", .{});}main.zig:8:13: error: error union is discarded _ = save(5); ~~~~^~~main.zig:8:13: note: consider using 'try', 'catch', or 'if'Same fixes: try, catch or if. If you really mean to ignore the failure, save(5) catch {}; says so openly.
value of type 'X' ignored
Section titled “value of type 'X' ignored”A close cousin: calling a function that returns a normal value (no error) and not using it. square(4); alone gives:
main.zig:8:11: error: value of type 'i32' ignored square(4); ~~~~~~^~~main.zig:8:11: note: all non-void values must be usedmain.zig:8:11: note: to discard the value, assign it to '_'Use the result, or write _ = square(4); if you really don’t need it. Unlike error unions, normal values can be discarded with _.
Where you’ll usually see it
Section titled “Where you’ll usually see it”list.append(gpa, x),map.put(...)and other calls that allocate memory.- File and I/O calls: writing, flushing, opening, closing.
- Your own functions once you give them a
!return type. writer.print(...)andwriter.flush()on a real writer (unlikestd.debug.print, they can fail).
Related errors
Section titled “Related errors”- expected error union type, found ‘X’: the opposite mistake,
tryon a call that can’t fail. - expected type ‘X’, found ‘Y’: storing a
!Tresult as a plainTwithouttry. - unused local variable: the same “use what you made” rule for names.
- All Zig error pages: the full list.
Practice
Section titled “Practice”1. Build a list
Section titled “1. Build a list”Fix this program so it prints { 10, 20, 30 }. Use try.
const std = @import("std");
pub fn main(init: std.process.Init) void { var list: std.ArrayList(u32) = .empty; defer list.deinit(init.gpa); for ([_]u32{ 10, 20, 30 }) |n| { list.append(init.gpa, n); } std.debug.print("{any}\n", .{list.items});}Hint: two lines change: the call, and the return type of main.
Show solution
const std = @import("std");
pub fn main(init: std.process.Init) !void { var list: std.ArrayList(u32) = .empty; defer list.deinit(init.gpa); for ([_]u32{ 10, 20, 30 }) |n| { try list.append(init.gpa, n); } std.debug.print("{any}\n", .{list.items});}Output:
{ 10, 20, 30 }try passes an OutOfMemory error up to main, and !void lets main return it. The defer still frees the list on the way out, whether main succeeds or fails.
- A
!in the return type means the call can fail. Zig won’t let you ignore that. trypasses the error up (and the function needs!in its own return type).catch |err| { ... }handles it here;catch {}ignores it openly;if ... else |err|branches._ = call();giveserror union is discarded:_doesn’t work for errors.- The long
@typeInfo(...)type in the message just means “an inferred error set”. Look at the part after the last!.
Next up: expected error union type, found ‘X’, the error for a try that has nothing to catch.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Errors in the Zig language reference: error sets, error unions and how to handle them.
- catch in the Zig language reference: fallbacks and
catch |err|. - try in the Zig language reference: passing errors up.
- Zig Try and Catch: our lesson with more examples.