无法在Spring Boot pubsub应用程序中正确处理异常

问题描述 投票:0回答:1

这是我遇到问题的代码:

try {
    publisher.publish(payload).get();
} catch (com.google.api.gax.rpc.DeadlineExceededException e) {
    LOGGER.error("com.google.api.gax.rpc.DeadlineExceededException occured: " + e.getCause());
} catch (com.google.api.gax.rpc.NotFoundException e) {
    LOGGER.error("com.google.api.gax.rpc.NotFoundException occured: " + e.getCause());
} catch (com.google.api.gax.rpc.ApiException e) {
    LOGGER.error("com.google.api.gax.rpc.ApiException occured: " + e.getCause());
} catch (io.grpc.StatusRuntimeException e) {
    LOGGER.error("io.grpc.StatusRuntimeException occured: " + e.getCause());
} catch (java.lang.RuntimeException e) {
    LOGGER.error("RuntimeException occured: " + e.getCause());
} catch (java.util.concurrent.ExecutionException e) {
    LOGGER.error("java.util.concurrent.ExecutionException occured: " + e.getCause()); // my program cursor always come to this point
} catch (Exception e) {
    LOGGER.error("Exception occured: " + e.getCause());
}

并且ExecutionException catch的getCause为:

java.util.concurrent.ExecutionException发生:com.google.api.gax.rpc.NotFoundException:io.grpc.StatusRuntimeException:NOT_FOUND:找不到资源

[如果您看到我已经将“ com.google.api.gax.rpc.NotFoundException”的捕获放入第二个捕获中,那么为什么它进入了ExcutionException捕获。

由于这种性质,我无法为客户写正确的消息。

在此先感谢您能提供帮助。

spring-boot exception exceptionhandler
1个回答
0
投票

[publisher.publish(payload).get()将被执行,它将抛出java.util.concurrent.ExecutionException ex = new com.google.api.gax.rpc.NotFoundException(),即对java.util的引用.concurrent.ExecutionException,但com.google.api.gax.rpc.NotFoundException的对象。因此,在try catch中,当引用正在检查要去哪个catch块时,它无法通过api.gax.rpc.NotFoundException()获得,因为cactch仅捕获相同或超类,因此稍后会捕获,但由于object是由com组成的。 google.api.gax.rpc.NotFoundException,因此getCause被覆盖,并且在您执行它时调用了对象方法。

决议:例如

try{

}
catch(Exception e){
            throw (e.getClass()).cast(e);

}

您可以在java.util.concurrent.ExecutionException中放置此catch块的位置。

© www.soinside.com 2019 - 2024. All rights reserved.