首页 > 解决方案 > 尽管在方法声明中声明了已检查的异常,但在 Spring Retry 中抛出了 UndeclaredThrowableException

问题描述

编辑:我仔细阅读了链接的主题(Getting UndeclaredThrowableException 而不是我自己的异常),但它没有回答我的问题 - 该票建议在我已经做过的拦截方法中声明检查的异常。

我正在尝试使用 Spring Retry 机制并希望在 resolve 方法中抛出我的自定义检查异常,但它因 UndeclaredThrowableException 而失败。这是我所做的:

  1. 我已经声明了我的自定义 RuntimeException(这表明我需要重试):

public class RestCallRetryException extends RuntimeException

  1. 然后我在以下方法的实现中抛出这个运行时异常:

@Retryable(value = RestCallRetryException.class, maxAttempts = 2) InquiryResponse postInquiry(InquiryRequest inquiryRequest) throws RestCallException;

  1. 我已经声明了重试方法来处理它:

@Recover InquiryResponse recover(RestCallRetryException e) throws RestCallException;

  1. 如您所见,我还有 RestCallException,它是在恢复中抛出的已检查异常:

public class RestCallException extends Exception

我在recover(RestCallRetryException e) 的实现中抛出RestCallException 并得到UndeclaredThrowableException。我不明白为什么会这样。如果我没有声明标有 @Retryable (postInquiry) 的方法会引发 RestCallException,我会理解这种情况——但这一切都已声明,正如您在我的代码片段中看到的那样。我在这里想念什么?

标签: javaspringexceptionaopspring-retry

解决方案


推荐阅读