Trying to learn Zig, but not understanding quite how to handle errors. I have this init function that I want to validate the input parameters to, if the values are invalid I want to raise an error.
In test “Valid limits” I call the function with try Limits(i32).init(100, 20);, however, I can’t do that in test “Invalid limits” because I get the error error: expected error union type, found regulators.pid.Limits(i32). Which is why I have const limit = Limits(i32).init(20, 100);. That works.
So I’m assuming I test the limit variable wrong? Since I probably should call const limit = try Limits(i32).init(20, 100); in “Invalid limits” test also.
(Side note: Python user describes their code as “Pythonic” when its well written, does Zig have a similar lingo?)
try Limits(i32).init(...) is equivalent to Limits(i32).init(...) catch |err| return err;, so @TypeOf(limit) after try Limits(i32).init would be Limits(i32) (the error would have already been handled by the try, i.e. the test would fail if the function returned an error), while the @TypeOf(limit) after Limits(i32).init (without the try) would be LimitError!Limits(i32) (the error information would still be available and would need to be handled in some way before being able to use the Limits(i32))
Zig does not allow you to implicitly ignore or forget about handling errors. Consider for example the error message when attempting to discard an error:
error: error is discarded
_ = canError();
~~~~~~~~^~
note: consider using 'try', 'catch', or 'if'
Similarly, storing error unions in memory via a const/var might allow you to forget (ironically) to deal with the error and as such Zig doesn’t allow it. You are however allowed to explicitly ignore errors if you wish, with catch {} or catch unreachable
If you want to discard error conditions if they can occur, you should opt for “catch { }” because it explicitly states “do nothing if we get an error”. That’s different than saying “this block of code should never be reached”.
Of course I’m going to mark the answer made by @squeek502 as the solution to this question. Feel free to open adjacent threads if you have more questions.
A solution that answers one of my questions,
having functions used in a routine, well loop, there I can use it and put catch .{} , but it will still throw the error just that in my internal routine I don’t need to handle it.
i think i am right??? .
if true, I will resume my project
hmm, it’s more complicated than that catch{} doesn’t work everywhere