by sham1 · 1w ago · 0 comments
I guess I have to justify my last statement, so here goes. We need to first establish the fact that checked exceptions and an Either type used in error handling are isomorphic constructs. Both are part of your type system (there is no difference between a function returning an `Either<T, E>` and a function returning a `T` or throwing an exception `E`), although one could correctly argue that an Either type is more widely applicable. Okay, so error handling ergonomics. The main thing about exceptions is that they separate out your happy path code from error handling. This is often a physical separation via having separate blocks with a try and a catch. You can of course have this kind of separation with an Either type, but at least in my experience the ergonomics are such that it's often nicer to just have the error boundary close to the fallible expression. Either that, or you have to start doing stack unwinding until you find an appropriate place for error handling -- except unlike with exceptions, this unwinding is done by hand.
Another thing that most exception systems give you are post-operations, usually in the form of some sort of a `finally` type dealio. And yeah, you can absolutely do the same thing with Either-based error handling, but it's not nearly as ergonomic as a finally-block.
And if people get annoyed at these being called exceptions, because they're not always exceptional, then we could call them something like "error conditions" or whatever floats one's banana, that's just bikeshedding.
The place where Java went wrong with its checked exceptions was two-fold:
a) you can't parameterise properly over the exceptions you get thrown b) whether an exception is checked or not is a property of its place in the class hierarchy instead of being separate from it
A large part of the first one is due to generics being added after the fact to the language, trying to preserve backwards compatibility which also lead to the kind of type erasure etc. you get in Java. And the second one is mostly just a mistake in the design of the exception hierarchy. Sadly that's now impossible to fix in Java specially so we're stuck with it, but other languages could still learn from it.
No comments yet.