by icholy · 4y ago · 0 comments
let foo;
try {
foo = await is_this_better();
} catch (err) {
console.log("if err != nil doesn't seem so bad all of a sudden");
} func ThisCouldUseExceptions() error {
err := Exceptions()
if err != nil {
return terrors.Propagate(err)
}
err = GeneraliseBetter()
if err != nil {
return terrors.Propagate(err)
}
value, err = InFunctionsWithMultipleErrorPoints()
if err != nil {
return terrors.Propagate(err)
}
value, err := AlsoTheyHelp()
if err != nil {
return terrors.Propagate(err)
}
chained_val, err := WithChaining(value)
err = AndPreventAccidentallyIgnoringErrors(chained_val)
if err != nil {
return terrors.Propagate(err)
}
} func ThisCouldUseExceptions() error {
err := Exceptions(arg)
if err != nil {
return fmt.Errorf("if a '%v' dev: %w", err)
}
err = GeneraliseBetter()
if err != nil {
return fmt.Errorf("has to actually: %w", err)
}
value, err = InFunctionsWithMultipleErrorPoints()
if err != nil {
return fmt.Errorf("read these errors: %w", err)
}
value, err := AlsoTheyHelp()
if err != nil {
return fmt.Errorf("they will actually be: %w", err)
}
chained_val, err := WithChaining(value)
if err != nil {
return fmt.Errorf("useful: %w", err)
}
err = AndPreventAccidentallyIgnoringErrors(chained_val)
if err != nil {
return fmt.Errorf("this isnt a real problem: %w", err)
}
} if err != nil {
return mherr.WrapErr(err, "optional context")
}
I just wrote a custom function that adds the stack trace to the error. I also have it setup as a code snippet in VS Code so all I have to do is type "if err" and hit tab. Yes it looks a bit verbose but it adds approximately zero extra work and makes error handling extremely easy by default.Also, turn on log flags to add line numbers. log.SetFlags(log.Llongfile | log.LstdFlags)
receive
{lack_of_erlang_error_handling_is_a_missed_opportunity, true} -> ok
after 0 ->
throw let_it_crash
end.Most of the time when an exception occurs in sequential operations that can fail, you don't really care where it failed exactly, only that the failure was caught.
The irony is that Go does have half baked exceptions (panic,recover) on top of that error as value convention.
No Go doesn't do it better, Go just doesn't give you the choice, from a convention perspective. You have a discipline issue with Java, it is not an issue in the language itself, it's with you and your team.
Ironically I've seen a few Go libs and project trying to re-invent optional types due to the verbosity of Go errors, just like people were trying to roll their own generics with interface {} or code generation before they were added to the core. It's a demonstration that some developers don't like the status quo.
The encoding/json package in the Go standard library uses exceptions for error handling. You absolutely have a choice, but it's a choice you're not likely to want to make because exceptions are named exceptions for good reason: It's for exceptions, not errors. Those are very different things.
But sometimes the trade-offs of overloading something beyond its intent is worth it. In those cases do it. Balancing trade-offs is what engineering is all about.
Its my entire company, and its with the open source ecosystem. Its naive to think that language constructs dont influence dev's decisions. You need to make it easy to do the right thing, not hard, and go makes it easy, which java fails at.
Only if you consider the "language" to be only the language itself, i.e. syntax + semantics. When people refer to a "programming language", they almost always mean the whole package, i.e. the language itself, the community, package ecosystem, tooling, conventions, etc.
If there is widespread usage of an incorrect pattern, then it is the failure of the language for not addressing it. For example, C's lack of package/build management.
The experiences with exception-based languages others in this thread brought up mirror my own. No thinking about error handling whatsoever, empty `catch` blocks, dumping stacktraces in lieu of error messages, etc.
Needing discipline to prevent those is exactly the problem. The language is what should guide you towards a better way, in this case explicitly returning errors.