3 ms·
When learning Go, I actually liked how it dealt with errors. I like that you need to aknowledge that an error could happen, but I also like that it's a normal r
by Insanity 8y ago
When learning Go, I actually liked how it dealt with errors. I like that you need to aknowledge that an error could happen, but I also like that it's a normal return value.
It's not valid for _everything_ so I don't agree with it entirely. But, some things that are exceptions in Java would make more sense as just returns because it is _valid_ output for a function.
I think it's the distinction between "the method can't perform what it's supposed to perform due to the input it received" vs "something unexpected happened and that's why the method couldn't perform what it was supposed to".
I'll try to give an example:
Imagine you have a method in Java for parsing a credit card, which follows a certain pattern for verifying CSV. If the pattern wasn't satisfied, you could have a:
Throw new IllegalArgumentException("Input does not match CSV string ([0-9])")
But to me _exception_ would signify something unexpected happened. Whereas here I'd like if I could just 'return' that the input was not correct, and therefore the method could not check the CSV.
Whereas, for example, if you have a method that writes to a File and the file you provided does not have the right priviledge -> this is an Exception because the user could not expect this.
I'm having a hard time wording this as consisely as possible, I'm sorry if it seems a bit incomprehensive but I'll try to TL;DR:
Exceptions: Unexpected behaviour from system
Error returns: Wrong input from user
If that makes sense :)
- masklinn 8y ago> If that makes sense :) You're just trying to justify/rationalise that you like go's arbitrary positioning on the result/fault continuum (well not continuum, it's not really 1-dimensional), there are few positions which can't be justified there. > But to me _exception_ would signify something unexpected happened. Being provided random garbage when prompted for a CVV can reasonably be unexpected. > Whereas, for example, if you have a method that writes to a File and the file you provided does not have the right priviledge -> this is an Exception because the user could not expect this. Why would the user not expect ACL issues on something well known for involving ACLs?