0

I have a list of exception handlers for specific exceptions which I am trying to define through a map that I can then access later to invoke. However, the .apply(e) is complaining that

'apply(capture<? extends java.lang.Exception>)' in 'java.util.function.Function' cannot be applied to '(java.lang.Exception)'
private Response handleException(Exception e) {
    return HANDLERS.get(e.getClass()).apply(e);
}

Map<Class<? extends Exception>, Function<? extends Exception, Response>> HANDLERS =
    Map.ofEntries(
        Map.entry(SpecificException.class, (Function<SpecificException, Response>) e -> ...),
        Map.entry(Exception.class, (Function<Exception, Response>) e -> ...)
    );

I am sure this is a duplicate question but could not find anything related to solving this issue. Is there a solution to this specific error or is there another approach to this?

eric
  • 11
  • 3
  • 1
    How did you end up with a `Function extends Exception, Response>`? That's a function that can never be called, because you'd have to call it with a class that extends *every* subclass of `Exception` at once. – Silvio Mayolo Nov 05 '21 at 02:49
  • See https://stackoverflow.com/questions/21041142/method-in-the-type-mapstring-capture1-of-extends-object-is-not-applicable or https://stackoverflow.com/questions/57549932/question-about-java-generics-and-design-of-java-util-function-function – Arsh Coder Nov 05 '21 at 03:12

0 Answers0