-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: Go 2: simplify error handling with try err == nil {} except {} #33387
Comments
Note that this requires a change in the scoping rules. In code like try err == nil {
jsonFile, err := os.Open("people.json")
} the For example var err error
if true {
_, err := os.Open("does not exist")
fmt.Println(err)
}
fmt.Println(err) That program will print an error and then print So in order for this to work I think that either |
Could the scoping issue be cleaned up by moving the TryStmt = "try" [ SimpleStmt ";" ] Expression Block "except" Block . used as: var people People
try var err error; err == nil {
jsonFile, err := os.Open("people.json")
jsonBytes, err := ioutil.ReadAll(jsonFile)
err = json.Unmarshal(jsonBytes, &people)
} except {
return nil, errors.Wrap(err, "failed to read people.json")
} which would then translate to: var people People
{
var err error
jsonFile, err := os.Open("people.json")
if !(err == nil) {
goto Except
}
jsonBytes, err := ioutil.ReadAll(jsonFile)
if !(err == nil) {
goto Except
}
err = json.Unmarshal(jsonBytes, &people)
if !(err == nil) {
goto Except
}
goto NoExcept
Except:
return nil, errors.Wrap(err, "failed to read people.json")
NoExcept:
} |
This proposal doesn't allow unique per-statement error handling (annotation) which I and others consider a requirement. |
Again and again I see proposals with the same exact premise but don't remember ever needing that kind of error handling in practice. Where does that expectation comes from? This looks exactly like Swift do/catch and has the same problem for me - it's really rare that I use the same error handling code for multiple call sites. In Swift, lacking any other idiomatic way of handling errors, this forces me to constantly wrap code in do/catch just to add some context. Also the fact that do/try block creates another scope means that any variable declared in that scope is invisible to the catch/except block. That's very annoying and forces you to declare variables outside of do/catch. |
@gwax That suggested scoping is still not consistent with the rest of the language. Currently a block scope starts at the (Also, note that |
@ianlancetaylor it sounds like a clean solution, from a scoping standpoint, likely depends on something happening with #377 |
I liked the Handle-Proposal better than this, where you could define an error handler on the same level as the code being executed. What I am seeing here, is people wrapping whole function bodies and having the same handle for different errors, which, in your case, makes sense, but surely not everywhere. |
This proposal does not have strong support. The scoping issue remains unresolved. This is a likely decline. Leaving open for a month for final comments. |
There were no further comments. |
Premised on the expectation that people frequently want to execute a series of statements and check for errors after each statement but frequently want to respond to any error in the same way, we can construct a special statement block to execute these repeated checks for us.
Try statements
"Try" statements specify conditional transfer of control to an "except" clause based on repeated checks of a boolean expression within a block. If the expression evaluates to false after any statement within the block, execution is immediately passed to the "except" branch.
Example:
The above translates directly to:
And can be used to replace:
The text was updated successfully, but these errors were encountered: