为什么方法在异步嵌套的异常堆栈跟踪中没有出现两次?

时间:2019-11-15 11:34:32

标签: java concurrency

我正在使用下面的示例代码,其中CompletableFuture被多次链接,并且我想知道当引发异常时,为什么链接代码不会在堆栈跟踪中多次出现。 / p>

package com.yannick;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

public class AsyncExceptionNestingTestMain {
    public static void main(String[] args) throws Exception {
        CompletableFuture<String> failingCF = createFailingCF();
        CompletableFuture<String> chained = chainAndNestException(failingCF);
        CompletableFuture<String> chained2 = chainAndNestException(chained);
        chained2.get();
    }

    private static CompletableFuture<String> chainAndNestException(CompletableFuture<String> toChain) {
        return toChain.handleAsync((result, exception) -> {
            if (exception != null)
                throw new CompletionException(exception);
            return result;
        });
    }

    private static CompletableFuture<String> createFailingCF() {
        return CompletableFuture.supplyAsync(() -> {
            throw new RuntimeException("Failed!");
        });
    }
}

以下是例外:

Exception in thread "main" java.util.concurrent.ExecutionException: java.util.concurrent.CompletionException: java.util.concurrent.CompletionException: java.lang.RuntimeException: Failed!
    at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)
    at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)
    at com.yannick.AsyncExceptionNestingTestMain.main(AsyncExceptionNestingTestMain.java:11)
Caused by: java.util.concurrent.CompletionException: java.util.concurrent.CompletionException: java.lang.RuntimeException: Failed!
// here is the only occurence of the chaining method:
    at com.yannick.AsyncExceptionNestingTestMain.lambda$chainAndNestException$0(AsyncExceptionNestingTestMain.java:17)
    at java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:822)
    at java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:797)
    at java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:443)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: java.util.concurrent.CompletionException: java.lang.RuntimeException: Failed!
    at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
    at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1592)
    at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582)
    ... 4 more
Caused by: java.lang.RuntimeException: Failed!
    at com.yannick.AsyncExceptionNestingTestMain.lambda$createFailingCF$1(AsyncExceptionNestingTestMain.java:24)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
    ... 5 more

1 个答案:

答案 0 :(得分:1)

CompletionException将CompletionFuture作为特殊类型的Exception处理CompletionException:

        if ((x instanceof CompletionException) && (cause = x.getCause()) != null)
            x = cause;

因此,当您在代码中引发CompletionException时,仅报告其原因。如果替换,您可以看到链接的异常:

    throw new CompletionException(exception);

通过(例如):

    throw new RuntimeException(exception);