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

Spring boot 无法在spring boot pubsub应用程序中正确处理异常,spring-boot,exception,exceptionhandler,Spring Boot,Exception,Exceptionhandler,这是我遇到问题的代码: 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.NotF

这是我遇到问题的代码:

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捕获的getCause是:

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

如果你看到我已经把“com.google.api.gax.rpc.NotFoundException”的捕获放在了第二个捕获中,那么为什么它会进入ExcutionException捕获中呢

由于这种性质,我无法为客户编写适当的信息

如果有人能提供帮助,请提前感谢。

当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(),因为catch只捕获相同的或超类,所以它稍后得到catch,但由于对象是由com.google.api.gax.rpc.NotFoundException生成的,所以getCause被重写,并且在执行时调用了对象方法

决议:例如

try{

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

}

你可以把这个catch块放在java.util.concurrent.ExecutionException中。

好的,我可以理解,但是现在我如何让NotFoundException从ExceptionException抛出,有没有合适的方法来处理这种情况。catch(Exception e){Exception Exception=(e.getClass()).cast(e);throw Exception;}。试着这样做。我想捕获NotFoundException并只抛出NotFoundException not Exception only。请纠正我。感谢我们只抛出了异常,它进入了父类的异常捕获块,这不是我想要的行为。我确实使用了相同的行为,但它再次进入了异常类,我在使用后检查:throw(e.getClass()).cast(e);它只给我异常类的对象(即ExecutionException)。所以它将如何进入父类NotFoundException捕获块。