2 ms·
People only handle checked exceptions that way because Java the language hasn’t given them the tools to deal with them properly (Swift does a good job at this)
by vips7L 2y ago
People only handle checked exceptions that way because Java the language hasn’t given them the tools to deal with them properly (Swift does a good job at this) and historically people would check exceptions from their downstream function calls that they couldn’t handle rather than unchecking them.
Checked errors are good and are so important to have program correctness, but you need a way to easily escape them when necessary. Rust and Swift have both have realized this. Rust provides ? and Swift has try!, Java needs the same.
> Look at the JDK 8 streams library of an example of a library that (a) is awkward as hell because you can't
> stream.map(this::someMethodThatMightThrowAndException)
This is again Java the language not providing the necessary tools. Checked exceptions can work across high order functions: https://docs.scala-lang.org/scala3/reference/experimental/canthrow.html https://docs.scala-lang.org/scala3/reference/experimental/ca...
- PaulHoule 2y agoMy impression (based on two projects) is that Scala is snake oil. (1) We were trying to parallelize an easily parallelizable task in Scala. Except it would only use a fraction of the CPUs available and didn't always give the right answer. In three days I was no closer to getting the Scala solution using all cores, I was able to do it in three minutes with Java Executor. (2) I saw a somewhat larger code base that implemented a data processing pipeline. I was told by the eng manager that (a) we do code reviews and (b) we use monads for error handling. I guess we did, except it was the monad equivalent of try { something() } catch(SomeException x) {} most of the time which, once more, fits pattern of people writing exception handling code to silence the compiler. In principle something like monads could let you implement more complex error handling strategies (like Lisp) but so long as "a monad is a like a burrito and a computation is like a graph", monads will be underpowered. Note you can use polymorphism for handling errors in Java too, for instance interface FunctionThatThrows<In,Out,X> { Out applies(In arg) throws X; } JDK8 streams could have done better with the tools it had.