3 ms·
So how is this better, cleaner syntax than checked exceptions in Java, for example? I thought checked exceptions were considered a failed experiment. The conv
by jayd16 8y ago
So how is this better, cleaner syntax than checked exceptions in Java, for example? I thought checked exceptions were considered a failed experiment. The convention in go is to always handle the errors so isn't that the same thing?
Is it the lack of forced stacktrace that makes flow control through exception more palatable?
Do people just like convention over compiler enforced correctness?
Or is my understanding incorrect and devs don't really like go's error handling?
- erik_seaberg 8y agoThere's a difference between "errors should alter the flow of control" and "error types should be transitively declared". The latter failed in Java because the language didn't have enough support for handling the layering violation. E.g., if the X ctor throws suddenly you can't implement Supplier<X> without dirty hacks like writing an explicit handler that does nothing but rethrow an unchecked type.
- jayd16 8y agoThrowing in constructors is an anti-pattern imo but assuming you need it, how does go solve this? Would you not return nil and an error that you would immediately check? Seems like defensively checking return values in go isn't that much better than catching every exception. Maybe I don't understand the correct go implementation.
- skybrian 8y agoGo's error handling has flaws, but one thing it got right is having a single error type that everyone uses. Most of the time you don't think about what subtype it is. In Java, declaring a method to throw any Exception (or should it be Throwable?) is considered an antipattern and most people use subtypes. I think if the exception hierarchy were simpler so that most of the time, there are only two kinds of methods (those that always succeed and those that can fail), checked exceptions would have worked out.
- jayd16 8y agoThis doesn't seem like a significant difference to me. You can catch all throwables and handle them the same way. Ironically, it feels worse to ignore the added exception type information so it's rare to see errors handled generically this way.
- skybrian 8y agoWell, that's what I mean. You could sorta do it in Java, but people don't do it that way because there aren't consistent conventions and it feels wrong. But adding the extra type information exposes internal details and results in a backward-incompatible API change if you ever want to start throwing a new exception. Some people extend RuntimeException instead, which means you don't know whether a generic RuntimeException is recoverable or not. It's similar to how code formatting tools have been around a long time, but Go made their formatter standard, and that made all the difference. Conventions sometimes matter more than language features.