5 ms·
Checked exceptions just always felt annoying to work with. Imo Rust's Result type is more versatile
by h4x0rr 3y ago
Checked exceptions just always felt annoying to work with. Imo Rust's Result type is more versatile
- wvenable 3y agoChecked exceptions are just another return type with a weird syntax. It's not that Rust's result type is more versatile, it's just less syntax to accomplish what works out to be the same thing.
- jcparkyn 3y agoThe difference is more than just syntax, at least in the case of Java and Rust. The core problem with checked exceptions in Java (as demonstrated in most of the examples from TFA) is that they don't compose properly with generic functions like map. Checked exceptions exist in parallel to the type system, and other parts of the language don't have the capacity to deal with them. On the other hand, a Result type makes errors part of the type system, and all other code can work with them "by default".
- wvenable 3y agoI agree. Checked exceptions are just a worse version of Result types. I did not intend to make them sound better than they are. Unchecked exceptions, however, are a different beast entirely. They're not just checked exceptions without the checking; they fundamentally change how you code and how you think about error handling.
- ben0x539 3y agoWhoa, I've been having opinions about error handling and result types and checked exceptions for like years and I never considered that Java couldn't be "properly" polymorphic over checked exception types. Thanks for pointing that out.
- int_19h 3y agoThat's a specific choice that Java designers made, though, not something inherent to checked exceptions in principle. During Project Lambda, one of the proposals - Neal Gafter's - actually had a full-fledged generic exception specification facility that allowed you to define HOFs along the lines of "I throw everything X does, and also Y". They killed it because it was "too complicated".
- benjiweber 3y agoYou can use checked exceptions equivalently to Result types in streams e.g. https://github.com/unruly/control/blob/master/src/test/java/examples/ExceptionsInStreamsHandling.java https://github.com/unruly/control/blob/master/src/test/java/...