error: expected error union type, found '?[]u8'
const file_buffer = try stdin.readUntilDelimiterOrEofAlloc(allocator, '\n', 100) catch error.StreamTooLong;
It works if I remove the catch, but then I cannot handle an error.StreamTooLong.
I’m not really sure what to do from here to be able to handle the error.
Adding an orelse would only really handle a null return and not the error union, and if I can’t use a catch I don’t know how to unroll this into a graceful failure, or allow the user to retry the input.
you’re not using the catch syntax correctly. you might want to write something like catch |err| switch (err) and then put in code to handle the errors you expect to need to ask the user to retry for, for example.
First, your gpa should be var becuase you are modifiying it by using it.
Second, catch is not really used for catching exceptions, like other languages might use it in a try…catch expression. Catch is used to provide the default value for an expression when an error occurs. So the result of a catch expression should be of the correct type. try is used as shorthand to immediately return an error if it occurs. And you won’t usually use try and catch together. Usually it will be one or the other. Further info: Documentation - The Zig Programming Language
jeff@jeff-debian:~/repos/untracked$ zig run test1.zig
Enter a file: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Stream too long!
Good response in total and generally correct. One caveat here: catch does not have to strictly return a default value. You can do things like break assignments with it - same with orelse.
If you want a quick rule of thumb that quickly “catches” (see?) this as a red flag: try and catch will seldom (if ever) go together; you either try an expression or catch its possible error return.
Thanks. I see I let my python background leak into my code. So “try” is not required if an error is possible if you already have a catch in place to capture the error value?