-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Throwable functional interfaces #28417
Comments
Please consider throwing generic exception like this @FunctionalInterface
public interface CheckedFunction<T, R, E extends Throwable> {
R apply(T t) throws E;
} |
@quaff, can you please expound on your proposal with a rationale for needing the generic type declaration for the exception? |
I agree: such types can be very useful. I also agree with the I think we should go with Furthermore, the What we're really talking about is a "supplier that is capable of throwing a checked exception", but we cannot convert that to a type name because it's simply too long: |
@sbrannen For example we can throw Throwable inside lambda @Aspect
public class Instrumentation {
@Around("execution(* *.*(..))")
public Object timing(ProceedingJoinPoint pjp) throws Throwable {
return Tracing.execute(pjp, ProceedingJoinPoint::proceed);
}
} public class Tracing {
public static <T, R> R execute(T input, CheckedFunction<T, R, Throwable> function) throws Throwable {
// create new span
try {
return function.apply(input);
} finally {
// finish span
}
}
} |
Many projects use |
The Spring Framework typically tries to avoid throwing The commit Phil pushed declares
Indeed, that would be another option, but we've gone with |
Within the framework and portfolio projects we often need to deal with checked exceptions inside the body of a
Function
,Supplier
,Consumer
, etc. This usually involves writingtry
/catch
blocks inside the body. Since exception handling is usually also handled by the framework, it would be nice if we could offerThrowing...
versions of common functional interfaces that do the wrapping for us.The text was updated successfully, but these errors were encountered: