5

I am just learning Go programming language. I just read about the way of error handling in Go. There is a nice explanation but I have some confusion about it. The typical way of handing error in Go is just returning error as a second value of function:

f, err := Sqrt(-1)
if err != nil {
    fmt.Println(err)
}

Very simple. But let say I am a library developer and I might want to know the client that my function can throw some error nad client must handle it.

In Java, I have throws clause. So, client must have to put it in those try...catch blocks

In Go, I can return error from a function; but let say if client of library avoid to handle it then how do I gracefully tell him to handle it?

Edit: I am just exploring a language. Not trying to mix java's thinking with Go and respect everyone's philosophy of doing it. I just asked because I wanted to know if there is any keyword because I haven't learnt Go very well yet.

Krupal Shah
  • 8,949
  • 11
  • 57
  • 93
  • 1
    That's the philosophy of Go: If the developer sees that the function returns an error, it's then his responsibility to check it. The programming language won't force you to do it. – W.K.S Jun 21 '16 at 19:07
  • 4
    Checked exceptions are a feature unique to Java, no other programming language has them. In other words: there is no equivalent to a `throws` clause in Go or in any other programming language than Java. – Jesper Jun 21 '16 at 19:09
  • Also read: https://blog.golang.org/errors-are-values. It isn't your problem if a client doesn't handle an error you return. If the client _must_ handle it, then you document that. – JimB Jun 21 '16 at 19:09
  • @jimB I found many cases where I want to use throws clause to let client know - "hey there can be a error. you must have to handle it." That's also beneficial for the client. Only by documenting it; you can't put it on right way. This may be subjective by the way. – Krupal Shah Jun 21 '16 at 19:15
  • 3
    @KrupalShah: In Go, the fact that one of the return values is an error communicates that this function is not guaranteed to succeed. I can understand that this feels weird and overly-optimistic, especially for a Java developer but you'll get used to it. For me, I've always hated exceptions because they have to be handled at every nested level. – W.K.S Jun 21 '16 at 19:21
  • 4
    @KrupalShah: it is subjective, so the only real advice I can offer is "don't write Java in Go" – JimB Jun 21 '16 at 19:21
  • 1
    @JimB No I am just exploring a language. Not trying to mix java's thinking with Go and respect everyone's philosophy of doing it. I just asked because I wanted to know if there is any keyword because I haven't learnt Go very well yet. – Krupal Shah Jun 21 '16 at 19:22

2 Answers2

9

Your library should also pass the err back through its exposed functions. You'll notice that many of the methods in the std library have a return value and an error so people should be used to having to handle errors.

If you really want to have behavior that appears similar to java you can use panic(), but note in golang panic should only be used for unrecoverable errors, or programmer errors (not runtime errors that can be recovered from)

Either way you can't force a client to handle it. It is their responsibility.

Josh Wilson
  • 3,585
  • 7
  • 32
  • 53
7

That's the philosophy of Go: If the developer sees that a function returns an error, it's then his responsibility to check the error. The programming language won't force you to do it.

Rob Pike's reasoning behind this is that errors are not special and therefore, there is no need for a specialized language construct (i.e. try/catch) to handle them.

W.K.S
  • 9,787
  • 15
  • 75
  • 122