Skip to content
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

Closed
Runrioter opened this issue Jun 10, 2020 · 4 comments
Closed

Proposal: Make Go support exception handling #39498

Runrioter opened this issue Jun 10, 2020 · 4 comments
Labels
Milestone

Comments

@Runrioter
Copy link

@Runrioter Runrioter commented Jun 10, 2020

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.

  • Exception handling mode(like Java etc.)

We use two new keywords throw and check. If a function's last result value has an error type. We can check this function. If this function returns a no-nil error, it throws this error.

throw func SomeFunctionWithException(src, dst string) string {
  r := check os.Open(src) // throw implicitly
  defer r.Close()

  w, err := os.Create(dst)
  if err != nil {
    throw fmt.Errorf("look!!! : %v", err) // rethrow explicitly
  }
  defer w.Close()
  // ...
}
  • Go error check mode

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.

func SomeFunction() error {
  // SomeFunctionWithException is defined above
  _, err := handle SomeFunctionWithException("foo", "bar")
  if err != nil {
    return fmt.Errorf("look!!! : %v", err)
  }
}

Or invoke a throwable function in a throwable function

throw func SomeFunction() {
  // SomeFunctionWithException is defined above
  _ := SomeFunctionWithException("foo", "bar")

}

Example:

throw func CopyFileWithThrow(src, dst string) {
  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 := handle CopyFileWithThrow("foo", "bar")
  if err != nil {
    return fmt.Errorf("look!!! : %v", err)
  }
}
@gopherbot gopherbot added this to the Proposal milestone Jun 10, 2020
@gopherbot gopherbot added the Proposal label Jun 10, 2020
@martisch
Copy link
Contributor

@martisch martisch commented Jun 10, 2020

@golang golang deleted a comment Jun 10, 2020
@yiyus
Copy link

@yiyus yiyus commented Jun 10, 2020

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.

@Runrioter Runrioter closed this Jun 10, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jun 10, 2020

For what it's worth, I would say that Go actually has exception handling, via panic and recover. We choose to say that Go doesn't have exception handling more as a matter of programming style than as a matter of language functionality.

@Runrioter
Copy link
Author

@Runrioter Runrioter commented Jun 11, 2020

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.