ziglang/ziglang.org
14
29
Fork
You've already forked ziglang.org
31

Example code for "A fresh take on error handling" in overview is incorrect (old API?) #609

Open
opened 2026年04月30日 20:44:10 +02:00 by rainywhile · 1 comment

https://ziglang.org/learn/overview/#a-fresh-take-on-error-handling
It looks like the example code "12-errors-as-values.zig" is supposed to be showing that openFile() can return a runtime error, and that it's a compile-time error not to handle this. The zig build-exe output displayed underneath is an error message, but it's saying that cwd() isn't a real function in the std.fs struct (the documentation (https://ziglang.org/documentation/0.16.0/std/#src/std/fs.zig) seems to agree on this) and has nothing to do with runtime error-handling.

The next example seems to use Io.Dir.cwd().openFile() instead of std.fs.cwd().openFile().

It's possible I'm misunderstanding (I'm completely new to Zig and that's why I'm reading the overview), so sorry if I'm just confused about what this is trying to show!

https://ziglang.org/learn/overview/#a-fresh-take-on-error-handling It looks like the example code "12-errors-as-values.zig" is supposed to be showing that openFile() can return a runtime error, and that it's a compile-time error not to handle this. The zig build-exe output displayed underneath is an error message, but it's saying that cwd() isn't a real function in the std.fs struct (the documentation (https://ziglang.org/documentation/0.16.0/std/#src/std/fs.zig) seems to agree on this) and has nothing to do with runtime error-handling. The next example seems to use Io.Dir.cwd().openFile() instead of std.fs.cwd().openFile(). It's possible I'm misunderstanding (I'm completely new to Zig and that's why I'm reading the overview), so sorry if I'm just confused about what this is trying to show!

I agree, this seems incorrect. I'm also new to Zig, but shouldn't it look something like this instead?

conststd=@import("std");pubfnmain(init:std.process.Init)void{_=std.Io.Dir.cwd().openFile(init.io,"does_not_exist/foo.txt",.{});// Wrong, cannot do this}

Result:

test.zig:4:34: error: error union is discarded
 _ = std.Io.Dir.cwd().openFile(init.io, "does_not_exist/foo.txt", .{});
 ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.zig:4:34: note: consider using 'try', 'catch', or 'if'
referenced by:
 callMain [inlined]: /opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/start.zig:737:30
 callMainWithArgs [inlined]: /opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/start.zig:638:20
 main: /opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/start.zig:663:28
 1 reference(s) hidden; use '-freference-trace=4' to see all references
I agree, this seems incorrect. I'm also new to Zig, but shouldn't it look something like this instead? ```zig const std = @import("std"); pub fn main(init: std.process.Init) void { _ = std.Io.Dir.cwd().openFile(init.io, "does_not_exist/foo.txt",.{}); // Wrong, cannot do this } ``` Result: ``` test.zig:4:34: error: error union is discarded _ = std.Io.Dir.cwd().openFile(init.io, "does_not_exist/foo.txt", .{}); ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test.zig:4:34: note: consider using 'try', 'catch', or 'if' referenced by: callMain [inlined]: /opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/start.zig:737:30 callMainWithArgs [inlined]: /opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/start.zig:638:20 main: /opt/homebrew/Cellar/zig/0.16.0_1/lib/zig/std/start.zig:663:28 1 reference(s) hidden; use '-freference-trace=4' to see all references ```
Sign in to join this conversation.
No Branch/Tag specified
main
release-0.16.0
apraga/french
homersimpsons/fix/mobile-responsiveness
MarkLagodych/patch-1
Zirunis/patch-1
zmitchell/zmitchell/build-graph-docs
generalmimon/fix-unit-testing-examples
generalmimon/fix-code-snippet-in-0.15.1-relnotes
TheConnorJohnson/main
meeting-minutes
enkvadrat/update-curl-example
No results found.
Labels
Clear labels
bounty
https://ziglang.org/news/announcing-donor-bounties
bug
Observed behavior contradicts documented or intended behavior.
contributor-friendly
This issue is limited in scope and/or knowledge of project internals.
downstream
An issue with a third-party project that uses this project.
enhancement
Solving this issue will likely involve adding new logic or components to the codebase.
infra
An issue related to project infrastructure, e.g. continuous integration.
optimization
A task to improve performance and/or resource usage.
question
No questions on the issue tracker; use a community space instead.
regression
Something that used to work in a previous version stopped working
upstream
An issue with a third-party project that this project uses.
use case
Describes a real use case that is difficult or impossible, but does not propose a solution.
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ziglang/ziglang.org#609
Reference in a new issue
ziglang/ziglang.org
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?