How can I find the Custom Exception class in CompletionException?

20 views Asked by At

I'm throwing multiple custom exception in CompletetionStage.failedFuture(), But failedFuture will always throws CompletionException.

If i throw custom Exception How can i find it's class ?

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

class Scratch {
    public static void main(String[] args) throws InterruptedException {
        CompletionStage<Integer> xx = dummy();
        CompletionStage<Integer> yy = xx.thenApply(y->{
            System.out.println(y);
            return y+99;
        }).exceptionally(ee->{
            ee.printStackTrace();
            if(ee instanceof CustomException){
                System.out.println("Custom Exception ");
            }else{
                System.out.println("Generic CompletionException");
            }
            return  -1;
        });


        Thread.sleep(10000);
    }



    public static CompletionStage<Integer> dummy(){
        if(1==0){
            return CompletableFuture.completedFuture(10);
        }
        return CompletableFuture.failedFuture(new CustomException());
    }
}
class CustomException extends Exception{}
0

There are 0 answers