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: Make Go support exception handling #39498
Comments
Relevant FAQ entry: https://golang.org/doc/faq#exceptions |
You are adding as many keywords as in the old check/handle proposal, but with a more limited functionality. In fact, all your examples can be written using only check: // Exception handling mode
func SomeFunctionWithException(src, dst string) (string, error) {
r := check os.Open(src) // throw implicitly
defer r.Close()
w, err := os.Create(dst)
if err != nil {
check fmt.Errorf("look!!! : %v", err) // rethrow explicitly
}
defer w.Close()
// ...
}
// Go error check mode
func SomeFunction() error {
// SomeFunctionWithException is defined above
_, err := SomeFunctionWithException("foo", "bar")
if err != nil {
return fmt.Errorf("look!!! : %v", err)
}
}
// Invoke "trhowable funcion"
func SomeFunction() error {
// SomeFunctionWithException is defined above
check SomeFunctionWithException("foo", "bar")
}
// Example
func CopyFileWithThrow(src, dst string) error {
r := check os.Open(src)
defer r.Close()
w := check os.Create(dst)
defer w.Close()
check io.Copy(w, r)
}
func RebuildError() error {
err := CopyFileWithThrow("foo", "bar")
if err != nil {
return fmt.Errorf("look!!! : %v", err)
}
} Calling it exception handling does not help. |
For what it's worth, I would say that Go actually has exception handling, via |
@ianlancetaylor Thanks for your clarifying. In fact, my proposal just like exception, but it is not. It is a way to make us coding without checking error explicitly |
We know there are many proposals about Go error checking design problem. I learn a lot from these proposals. I have an idea. I think that we maybe make Go has two error handling modes. So, we need a way to convert Go functions between two modes.
We use two new keywords
throw
andcheck
. If a function's last result value has anerror
type. We cancheck
this function. If this function returns a no-nil error, it throws this error.For running a throwable function in a Go normal function, we use
handle
keyword to handle it, at the same time convert it a normal function.Or invoke a throwable function in a throwable function
Example:
The text was updated successfully, but these errors were encountered: