|
|
|
|
|
by twic
2377 days ago
|
|
You can do that and propagate the exception: @FunctionalInterface
interface ThrowingFunction<T, R, E extends Exception> {
R apply(T argument) throws E;
}
static <T, R, E extends Exception> R apply(T argument, ThrowingFunction<T, R, E> function) throws E {
return function.apply(argument);
}
But not catch and rethrow it, because throw and catch aren't generic, and the type parameter isn't reified. You can emulate reified generics with the usual trick of passing a Class object: static <T, R, E extends Exception> R apply(T argument, ThrowingFunction<T, R, E> function, Class<E> witness) throws E {
try {
return function.apply(argument);
} catch (Exception e) {
throw witness.cast(e);
}
}
But this is pretty horrid. |
|