I’ve seen the “clean up errors yourself” argument before, but, like the author, I don’t think it holds water. Often the correct response to errors is to panic() or pass it up the stack so the caller can deal with it or—more likely—panic() itself.
I’ve seen the “clean up errors yourself” argument before, but, like the author, I don’t think it holds water. Often the correct response to errors is to panic() or pass it up the stack so the caller can deal with it or—more likely—panic() itself.
fn canError(foo: bool) !void {
if (foo) {
return error.Foo;
} else {
return error.Bar;
}
}
test "canError" {
canError(true) catch |e| switch (e) {
error.Foo => @panic("foo!"),
};
}
Running `zig test` on that file would give: ./tmp/errors.zig:10:30: error: error.Bar not handled in switch
canError(true) catch |e| switch (e) {
^You get compile errors if you try to handle an impossible error, or don't include an `else` prong (in which case the compiler tells you the full set of unhandled errors).
Unfortunately most common languages does not prioritize this. As a contractor I have dug myself thru a lot of awful code in different languages and the most common denominator is improper error handling.
>He posted a follow up about error handling......
When I was reading the original blog I was thinking if he had any follow up, so I decide to click on archive and the homepage. And this article, somehow doesn't show up in both list. As a matter of fact I couldn't even find how to get to this blog post without your direct linking. Then it turns out it is a "FUTURE POSTS".
How does that work and why? Is this suppose to be some sort of preview before it is officially published?
The author posted the link in the comments of the original post.
That is a great idea. Thank You I might start doing that as well.
Handling Errors causes us developers so confusion, because we are so used to exceptions and how poorly they were conceived.
Errors should be called Failures. It's just when a function fails to so what it says it does. Nothing more, nothing special about it!
If you call fopen() and it doesn't find the file, that's a Failure! No reason to panic! Just go create the file!