3 ms·
> Java has a feature called checked exceptions that ensure that you handle all possible exceptions in your program, although admittedly this feature is annoying
by twic 10y ago
> Java has a feature called checked exceptions that ensure that you handle all possible exceptions in your program, although admittedly this feature is annoying.
I think it's mostly annoying because checked exceptions were used badly in the early years of Java, so there are methods which throw loads of unrelated exceptions which have to be handled separately. Better design of exception hierarchies, and the availability of multi-catch, makes this quite a bit better.
Although it's recently got worse again because none of the shiny new stream stuff supports lambdas which throw checked exceptions. You have to either stick to unchecked exceptions and lose type safety, or probably better, rotate the exceptions sideways in type-space and use an Either:
https://github.com/poetix/ambivalence/blob/master/src/main/java/com/codepoetics/ambivalence/Tryable.java https://github.com/poetix/ambivalence/blob/master/src/main/j...
- masklinn 10y ago> I think it's mostly annoying because checked exceptions were used badly in the early years of Java, so there are methods which throw loads of unrelated exceptions which have to be handled separately. I see that as a secondary issue, I feel the primary issue of checked exceptions is that handling and manipulating them is a terrible experience and even today I don't believe you can be generic over checked exceptions (though I could be wrong). > Although it's recently got worse again because none of the shiny new stream stuff supports lambdas which throw checked exceptions. It's not new though, the type-level facilities lambdas use simply don't allow for that. That's been an issue since generics were added, and even since the original release of Java. Checked exceptions can only go through stack frames which specifically knows about them. And that, I think, is why they're such an awful experience and a failed experiment.
- twic 10y agoI 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.