2 ms·
No, I have never written exception-handling code like that mocked in this article. There's a strong correlation between uses of 'catch' and the probability that
by barrkel 15y ago
No, I have never written exception-handling code like that mocked in this article. There's a strong correlation between uses of 'catch' and the probability that you're using exceptions incorrectly. Go's lack of exceptions is probably what pushes me away from it most strongly.
- tsotha 15y agoThat's my take as well. Most of the code I write doesn't attempt to recover from error conditions. I would say at least 85% of the time I want the program to exit with enough information that I can tell what happened. So I propagate the exceptions up to the top level. Nothing could be easier. This may all be a function of the environment in which my code runs, but I certainly have no interest in going back to my C days when 95% of the source was devoted error handling.
- deleted 15y ago[deleted]
- ericbb 15y agoGo doesn't lack exceptions. It just uses some different names (throw -> panic, catch -> recover) and uses a different way to write the code that catches them. http://golang.org/doc/go_spec.html#Handling_panics http://golang.org/doc/go_spec.html#Handling_panics Edit: Also, here's an interesting pattern to illustrate that Go's panics are a little different. func parse(s string, config *Config) (x Thing, err error) { defer config.RecoveryPlan(&x, &err) x, err = doParse(s) } It's contrived, but you can see that the "RecoveryPlan" can choose to ignore panics or recover from them and it can also affect the return value of the "parse" call by assigning to its parameters and recovering from the panic.
- barrkel 15y agoOh, I know about this already. Trouble is, panics aren't supposed to cross module boundaries. "The convention in the Go libraries is that even when a package uses panic internally, its external API still presents explicit error return values" http://blog.golang.org/2010/08/defer-panic-and-recover.html http://blog.golang.org/2010/08/defer-panic-and-recover.html But crossing interfaces between modules is, IMO, core to exceptions. They're ideally suited to communicating the underlying cause of failure from the depths of the system to the top-most loop (usually a request/response dispatcher or UI event loop), where the error message can be logged or shown to the user, as required, indicating the nature of the failure. I expounded further on this point a few years ago, related to Java's misadventure with checked exceptions, but also relevant to module crossing: http://cafe.elharo.com/programming/bruce-eckel-is-wrong/#comment-476790 http://cafe.elharo.com/programming/bruce-eckel-is-wrong/#com...
- ericbb 15y agoI won't get into the interface-crossing issue except to say that exceptions to the convention do exist. For example: http://golang.org/pkg/regexp/#MustCompile http://golang.org/pkg/regexp/#MustCompile