Exception handling with Consumer functions in Java 8

ahoffer

This code gives me a compile error on the line processBatch(batch, this::backupMetacard); The process batch method wraps the consumer in a try/catch block, but Java will not compile the call.

private synchronized void drain() {

    for (List<Metacard> batch : Lists.partition(metacards, BATCH_SIZE)) {
        getExecutor().submit(() -> {

            processBatch(batch, this::backupMetacard);
        });
    }
    metacards.clear();
}

void processBatch(List<Metacard> metacards, Consumer<Metacard> operation) {

    List<String> errors = new ArrayList<>();
    for (Metacard metacard : metacards) {
        try {
            operation.accept(metacard);
        } catch (IOException e) {
            errors.add(metacard.getId());
        }
    }

    if (!errors.isEmpty()) {
        LOGGER.info("Plugin processing failed. This is allowable. Skipping to next plugin.",
                pluginExceptionWith(errors));
    }
}

private void backupMetacard(Metacard metacard) throws IOException {...}
Tunaki

The problem is that in the following snippet, the method backupMetacard declares to throw the checked IOException.

getExecutor().submit(() -> {
    processBatch(batch, this::backupMetacard);
                        ^^^^^^^^^^^^^^^^^^^^ // <-- this throws a checked exception
});

As such, it does not comply anymore with the contract of the functional method of Consumer, which is apply and doesn't declare to throw checked exceptions.

Wrap this into a try-catch, where you can throw an unchecked exception instead UncheckedIOException:

getExecutor().submit(() -> {
    processBatch(batch, metacard -> {
        try {
            backupMetacard(metacard);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Exception Handling Java

分類Dev

Exception handling in Procedure with nested functions in pl/sql

分類Dev

Global Exception Handling in play framework java 2.3.7

分類Dev

In Java, can you consolidate identical exception handling logic to one place?

分類Dev

Throw a custom exception in optional in Java8

分類Dev

Handling a timeout exception in Python

分類Dev

Handling a custom exception

分類Dev

Handling an exception and continuing a loop

分類Dev

Is order of exception handling important?

分類Dev

Handling Jackson Exception

分類Dev

Disable Exception Handling

分類Dev

exception (handling) in c#

分類Dev

class for handling custom exception

分類Dev

Better way of error handling in Kafka Consumer

分類Dev

Handling nested if/else statements using Java 8 streams

分類Dev

handling nested callback functions

分類Dev

Kafka Consumer with JAVA

分類Dev

Java Producer Consumer IllegalMonitorStateException

分類Dev

Uncaught Error/Exception Handling in Swift

分類Dev

Correct extent of the exception handling block

分類Dev

Laravel 5.5 custom exception handling

分類Dev

C++ advice on exception handling

分類Dev

Python Selenium: Handling Webdriver exception

分類Dev

Behaviour of exception handling in constructor of a class

分類Dev

Spring MVC Generic Exception Handling

分類Dev

Exception handling in Symfony 2 Crawler

分類Dev

Java 8 java.util.function.Consumer <>に相当するc#は何ですか?

分類Dev

Java 8 java.util.function.Consumer <>に相当するc#は何ですか?

分類Dev

Cloud Functions for Firebase error handling