3 ms·
I don't think handling and manipulating checked exceptions is a terrible experience. Specifically, i don't think it's worse than using an Either, and as far as
by twic 10y ago
I don't think handling and manipulating checked exceptions is a terrible experience. Specifically, i don't think it's worse than using an Either, and as far as i know, that's the only other way to safely handle methods which can fail. The other options get better ergonomics by sacrificing safety, which isn't okay.
You can be somewhat generic over checked exceptions. This is okay:
@FunctionalInterface
interface RiskyGetter<T, E extends Exception> {
public T get() throws E;
}
This is not:
public <T, E extends Exception> T get(RiskyGetter<T, E> getter) {
try {
return getter.get();
} catch (E e) { // <-- can't catch a type parameter
e.printStackTrace();
return null;
}
}
So you have to both throw and catch concrete exception types, but you can have them pass through layers of generic code in between. It's not perfect, but it's something.
As a result, the streams stuff absolutely could have been written to handle exceptions. The standard Function interface could have been defined as throwing an exception, and the terminal operations could have thrown it on. Could, but i'm not saying should - the additional complexity of the interfaces would have been annoying, so the flexibility would not have been free.
- catnaroek 10y ago> Specifically, i don't think it's worse than using an Either, and as far as i know, that's the only other way to safely handle methods which can fail. `Either`s can be nested, put in lists, passed as arguments, and pretty much everything else you would expect from first-class values. Checked exceptions can't.