▲ 12 ▼ Proposal: A built-in Go error check function, try
We propose a new built-in function called try
, designed specifically to eliminate the boilerplate if
statements typically associated with error handling in Go. No other language changes are suggested. We advocate using the existing defer
statement and standard library functions to help with augmenting or wrapping of errors. This minimal approach addresses most common scenarios while adding very little complexity to the language. The try
built-in is easy to explain, straightforward to implement, orthogonal to other language constructs, and fully backward-compatible. It also leaves open a path to extending the mechanism, should we wish to do so in the future.
Register to comment or vote on this story
I really like this proposal. It is simple, minimal and addresses the main point of complaint which people have about error handling in go (verbosity).
I have no problem with encouraging errors to bubble up, as there is a clear flow of control and errors must be handled at the appropriate place in the chain (usually one or two levels up).
The one thing that I'd maybe change is to leave the working code alone and concentrating on the bits people actually don't like (the error handling boilerplate). So instead of a new try keyword, just have a new check keyword with similar semantics which just takes the error as an argument, and on non-nil error returns nil values and the error.
That would mean far fewer changes to code, things would read very much as they do now, but a bit less verbose, and as it's a function no new keywords required.