I have a bunch of methods, with similar signatures, compatible with Function, they each declare throws InterruptedException, and I put them in a list and select one by its index and call its apply().
Except that I get a compiler error where the method is named to put in the list:
        final List<Function<Set<BookIdentifier>, Set<Status>>>
                submitters = List.of(
                        this::submitIndividuallySynchronously, // these are the method references
                        this::submitInBulkSynchronously,       // all 4 of them have an error message
                        this::submitIndividuallyAsynchronously,
                        this::submitInBulkAsynchronously);
        final var submitter =
                submitters.get((isIndividual() ? 0 : 1) + (isSynchronous() ? 0 : 2));
and writing a try/catch later
       try {
            result = submitter.apply(work.todo);
        } catch (final InterruptedException ex) {
            // do something here
        }
where the method reference is used (called) doesn't help.
The error from javac (SDK 17.0.5) is
error: incompatible thrown types InterruptedException in functional expression
(IntelliJ gives different errors: Each method reference has an error Unhandled exception: java.lang.InterruptedException and the later catch block has an error Exception 'java.lang.InterruptedException' is never thrown in the corresponding try block.)
So how can I name these method references in one place and call them in another?
- Besides the obvious which is to surround the naming of them with a useless try/catch?
- That it's an InterruptedExceptionis not the issue, just needs to be any checked exception
