3 ms·
In Java its such a pain to always account for possible exceptions. You have explicitly catch it or explicitly pass it on. Scala frees you of this hassle by no
by ape4 11y ago
In Java its such a pain to always account for possible exceptions. You have explicitly catch it or explicitly pass it on. Scala frees you of this hassle by not enforcing anything. So handy.
- JackFr 11y agoAnd yet with the ease and availability of Try, Either and Option combined with fluent pattern matching I find myself writing much more robust code, especially when using legacy Java libraries. (My real fear is that Scala is just a gateway drug and if I don't take it easy I'll be main lining Haskell eventually. )
- incepted 11y agoSure, it's much easier to write code that doesn't handle errors, but you're creating buggy code. Checked exceptions force you to think about error cases upfront, that's their value. More pain for developers, better software for users.
- wtetzner 11y agoBut that's not really true. The point of exceptions is that they can bubble up without code in the middle having to know about them. If you want your caller to handle an error, then that should be part of the return type (e.g. sum types).
- incepted 11y agoBut then are doing the bubbling manually and, on top of that, you pollute the return type of all the methods along the way. Suddenly, callers of such a function that just needed an Int now have to deal with an Either<Error, Int> and all the boiler plate that comes with it. Exceptions fix that by not bothering any of these users because the return type of the methods in-between is not affected.
- ww520 11y agoYou can always use unchecked exception like RuntimeException and others to silently pass the exceptions back.
- catnaroek 11y agoExplicit exceptions in method signatures are a language design mistake. If you need to handle multiple classes of return values, that's what sum types are for. Exceptions exist to communicate secret messages from a raiser to a handler, preventing anyone in the middle from intercepting them.