在lambda中抛出Catch RuntimeException并将其重新抛出为checked

时间:2014-04-09 09:42:54

标签: java exception exception-handling lambda java-8

我想弄清楚为什么我的代码会抛出IllegalStateException,而不是我的自定义代码,我希望它抛出。

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);
    }
}

IllegalStateException被抛出:throw new IllegalStateException()

使用示例:

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);
    }
}

此代码应抛出NotASingleElementException,而不是抛出IllegalStateException,我怎么能让它工作?

做实际工作时出现错误行为:

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)

2 个答案:

答案 0 :(得分:2)

您的收藏家已退回,但未在listAndThenCollector方法中收集。实际的收集发生在:

.collect(CollectorUtils.singleElementCollector());

在您的测试方法中。那就是抛出异常的时候。

答案 1 :(得分:0)

我会说这是因为当你调用Collectors.collectingAndThen时,你传递了一个当时没有执行的lambda。当它最终执行时,你的捕获物就不再存在了。

你不能使用:

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