确定哪种类型的异常是可引发的

时间:2018-12-28 19:37:52

标签: java asynchronous exception throwable

这是当前正在运行的代码:

.whenComplete((r, throwable) -> {
    if (throwable != null) {
        logger.error("exception");              
    }
});

是否可以执行类似的操作来确定throwable是否是某种异常类型?

.whenComplete((r, throwable) -> {
    if (throwable == CertificateException) {
        logger.error("cert exception");             
    }
});

1 个答案:

答案 0 :(得分:3)

使用instanceof关键字查找类型

if (throwable instanceof CertificateException)

如果throwable与ExceptionThrowable之类的父级包裹在一起,则使用getCause()

if (throwable.getCause() instanceof CertificateException)