-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: introduce "throws" keyword for error handling #32852
Comments
This comment has been minimized.
This comment has been minimized.
This seems to be very similar to #32848. |
This comment has been minimized.
This comment has been minimized.
I hope to be rational, not polite. This recent “try” proposal allows info := try(try(os.Open(fileName)).Stat()) I can easily imagine me, senior, making such a mistake when I’m tired. And I bet juniors and even middles will do this all the time. |
Just throwing it out there ... and if we replace
( |
And we will have file descriptor leak. We need to call Close on each successful Open. |
@sirkon -- so the original "try" does
|
It leaks too. We don’t have a RAII in Go, so this essentially means a leak. That’s why try proposal is evil. Go type system is far from being able to handle this. This proposal gives nothing meanwhile: less lines but denser code. |
Thank you. Means (to expand a bit, I am thinking out loud here)
to be equivalent to
|
Yes, exactly |
(continue TOLH)
|
This is complicated. Better to invent RAII |
|
@tandr Go type system cannot even warn you about lack of resource release. It is already an issue even though we actually have sane syntax and semantics what softly directs us to care about it. |
This comment has been minimized.
This comment has been minimized.
That's the point, just return an error when applicable, and do nothing more to keep things simple.
That's true, but is to reduce the
As of its 'assign-on-the-left then execute-on-the-right' nature, we should not confuse the |
First of all, I prefer leaving
if err != nil
alone, it's quite good. But if we must add something for error handling, here's a new approach that behaves liketry()
but can handle more scenarios, seems to be clearer, and most importantly, keeps compatibility withif err != nil
ways.The proposal suggests to add a new keyword
throws
for error handling.The signature is simple:
or
where
NewBarError
returns a new error of typeBarError
which implements the standarderror
interface.In both cases, the last return value of enclosing function must be
error
interface type. iferr != nil
, the expression followingthrows
will take place and the enclosing function returns with the expression evaluation result as the value of its last return parameter(error
type) and its other return values are the type default values or the value of current corresponding variable(for named return parameters) .If
err == nil
, the expression afterthrows
will not execute and the program goes on.Last, the
throws
keyword is totally optional for error handling, you can omit it somewhere and use it at another place in the same enclosing function.Examples:
CopyFile
example from the overview becomes:I believe this approach may handle more scenarios than the try built-in proposal, especially when you need to wrap a new error for different function calls.
Drawbacks:
throws
may make the line longer(it depends).The text was updated successfully, but these errors were encountered: