#returning errors from main?
1 messages · Page 1 of 1 (latest)
(Sorry if that sounds really stupid, I've been trying for the past few minutes and gave up)
wdym?
you can return an error with !void
if it errors it'll exit with exit code 1
custom exit codes require the use of u8 or !u8
or std.os.exit :)
How?
here's an example ig:
(untested)
pub fn main() u8 {
myFunctionThatCanError() catch |err| return switch (err) {
error.Amongus => 420,
error.Bruh => 69,
else => 1,
};
return 0;
}
custom exit code per-error ^
just use try :)
pub fn main() !void {
try myFunctionThatCanError();
}
When I do
pub fn main() !u8 {
failingFunction() orelse return 1;
}
I get "expected type ..., got void"
How can I return either void or an u8?
make sure to add a return at the end
you can't from main
main only allows returning void, !void, u8, or !u8 iirc
np
not sure why you'd want void or a u8 tho
...How do I return an error from !void?
return error.MyError
pub fn myFunctionThatCanError() !void {
return error.MyError;
}
I mean you just return the error
MyError being the error name
what is MyError
it's a name
what name
oh wait i might get your confusion
okay so
- errors can't contain/carry values
- errors are named
whenever you create an error it's added to the global error set
Okay, which names can I use?
anything you want
this works now!
pub fn main() !void {
const sus = createAmogus() orelse return error.AmogusError;
}
oops I'm dumb
i don't think orelse works with errors, you'd have to use catch
Okay.
Thanks.
Sorry, the concept of returning errors that aren't numeric from main confused me