6 ms·
> Can I specify that implementations of foo can throw no, some, or all exceptions? What would it even mean to write something like throws *? Analogously, if I h
by cvoss 3y ago
> Can I specify that implementations of foo can throw no, some, or all exceptions? What would it even mean to write something like throws *? Analogously, if I have a function that takes a method as an argument, like a callback, how do I specify what set of exceptions in can throw? Can I have generic “exception set parameters”?
> Concretely, checked exceptions in Java failed because Java lacks “throwingness polymorphism”, if you will.
I don't understand. Every "missing feature" the author asks for is, in fact, present in Java. The example below just about sums it up.
@FunctionalInterface
interface MyCallback<X, Y, E extends Exception> {
Y apply (X x) throws E;
}
class A {
<X, Y, E extends Exception> Y runCallback (MyCallback<X, Y, E> func, X x) throws E {
return func.apply(x);
}
}
- tadfisher 3y agoThe author's point about the orthogonality of exceptions to values and types still stands; try creating a class like abstract class GenericException<R> extends Exception { R reason(); } As it is, Exception is a "special" type that isn't allowed to be used like normal Java types, because Java generics are a leaky abstraction introduced after checked exceptions, and the JVM cannot deal with type erasure in catch blocks.