简体   繁体   中英

Catch RuntimeException thrown in a lambda and rethrow it as checked

I am trying to figure out why my code is throwing a IllegalStateException , and not my custom one, which I hoped it to throw.

public final class CollectorUtils {
    private CollectorUtils() {
        throw new UnsupportedOperationException();
    }

    public static <E, R, X extends Throwable> Collector<E, ?, R> listAndThenCollector(final Predicate<List<E>> listPredicate, final Function<List<E>, R> listFunction, final Function<Throwable, X> exceptionWrapper) throws X {
        Objects.requireNonNull(listPredicate);
        Objects.requireNonNull(listFunction);
        Objects.requireNonNull(exceptionWrapper);
        try {
            return Collectors.collectingAndThen(Collectors.toList(), list -> {
                if (!listPredicate.test(list)) {
                    throw new IllegalStateException(); //Line that throws the exception
                }
                return listFunction.apply(list);
            });
        } catch (IllegalStateException ex) {
            throw exceptionWrapper.apply(ex);
        }
    }

    public static <E> Collector<E, ?, E> singleElementCollector() throws NotASingleElementException {
        return listAndThenCollector(list -> list.size() == 1, list -> list.get(0), NotASingleElementException::new);
    }
}

The IllegalStateException gets thrown at the line: throw new IllegalStateException() .

Example usage:

public static void test() {
    try {
        Integer result = IntStream.range(0, 2)
                .boxed()
                .collect(CollectorUtils.singleElementCollector());
    } catch (NotASingleElementException ex) {
        Logger.getLogger(CollectorUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
}

This code should throw a NotASingleElementException , instead it throws an IllegalStateException , how could I get it to work?

A stracktrace of the wrong behaviour when doing an actual job:

Exception in thread "pool-3-thread-1" java.lang.IllegalStateException
    at dpc2.base.utils.CollectorUtils.lambda$listAndThenCollector$0(CollectorUtils.java:28)
    at dpc2.base.utils.CollectorUtils$$Lambda$21/2071035411.apply(Unknown Source)
    at java.util.function.Function.lambda$andThen$6(Function.java:88)
    at java.util.function.Function$$Lambda$22/63121782.apply(Unknown Source)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:503)
    at dpc2.base.utils.ImageMagickUtils.convertPDFToTIFF(ImageMagickUtils.java:30)
    at dpc2.server.convert.ConvertConsumer.accept(ConvertConsumer.java:20)
    at dpc2.server.convert.ConvertConsumer.accept(ConvertConsumer.java:14)
    at dpc2.base.checker.BaseChecker.lambda$null$0(BaseChecker.java:116)
    at dpc2.base.checker.BaseChecker$$Lambda$15/2121862243.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:744)

Your collector is returned but not collected in the listAndThenCollector method. The actual collection happens with:

.collect(CollectorUtils.singleElementCollector());

which is in your test method. That's when the exception is thrown.

I'd say it's because when you call Collectors.collectingAndThen you pass it a lambda which does not execute at that time. When it ultimately executes your catch is no longer there to catch it.

Couldn't you use:

//throw new IllegalStateException();
throw exceptionWrapper.apply(new IllegalStateException());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM