3 ms·
Go's error handling is honestly the best I've ever used. No errors go unhandled. Period. get an error? Return it or deal with it. Makes much more sense than exc
by donatj 12y ago
Go's error handling is honestly the best I've ever used. No errors go unhandled. Period. get an error? Return it or deal with it. Makes much more sense than exceptions suddenly breaking the behaviour of the language.
- sfk 12y agoHow are errors in deferred functions handled? How about division by zero?
- NateDad 12y agoerrors in deferred functions are handled just like they would be if the function were not deferred. There are some things like division by zero and array index out of bounds that cannot be used with this method without making the language incredibly cumbersome... for those, there are panics (basically like exceptions in other languages)... but they happen quite rarely in practice.
- Jayschwa 12y agoDeferred functions can pass along errors by using a closure. http://play.golang.org/p/_ZqSk470-b http://play.golang.org/p/_ZqSk470-b
- politician 12y agoIt's not actually true that no errors go unhandled. func foo() error { return errors.New("whoa!") } func bar() { _ := foo() // onward! }
- NateDad 12y agoUh yes? This is you telling the compiler specifically that you don't care about the error. It's equivalent to try { foo() catch {} In other languages. That's not a problem, that's the computer doing what you explicitly told it to do.
- kornish 12y agoNot quite: here's an example of code which returns an error but for which the return value is never even captured. http://play.golang.org/p/EL2-2gl7wh http://play.golang.org/p/EL2-2gl7wh
- threeseed 12y agoI think you have a misunderstanding of exceptions. In JVM languages for example you have two types: checked and unchecked. Checked exceptions force you to handle errors immediately just like Go. Unchecked doesn't. There are many situations in which you want errors to go unhandled and bubbled up to some common error handling logic. Exceptions IMHO are vital. They standardise error handling behaviour across your application and provide flexibility in when, where, and how you handle groups of errors.