首页 > 解决方案 > 在 Spring Boot 中在 @ExceptionHandler 中抛出另一个异常不起作用

问题描述

我有一个我处理的 CustomException@ExceptionHandler(CustomException.class)

在一种情况下,我会InvalidFormatException从那里得到一个所以我需要重新抛出我的 CustomException。我尝试抛出异常,但它没有按预期工作

    @ExceptionHandler(InvalidFormatException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Error invalidFormat(InvalidFormatException e) throws CustomException {
      if (logicToValidate()) {
        throw new CustomException();
      } else {
        return new Error(BAD_REQUEST.name(), e.getMessage(), ErrorLevel.ERROR);
      }
    }

    @ExceptionHandler(CustomException.class)
    public Error customException(CustomException e) {
      ...
    }

标签: javaspring-bootexception

解决方案


在做了一些研究之后,我发现了这个线程https://github.com/spring-projects/spring-framework/issues/18299。所以现在不可能在 spring boot 中重新抛出异常。相反,我们可以尝试替代策略,如果有的话。


推荐阅读