简体   繁体   English

在春季启动时在@ExceptionHandler 内抛出另一个异常不起作用

[英]Throwing another exception inside @ExceptionHandler in spring boot is not working

I'm having a CustomException which I handle with the @ExceptionHandler(CustomException.class)我有一个 CustomException,我用@ExceptionHandler(CustomException.class)

In one case I'll be getting a InvalidFormatException so from there I need to rethrow to my CustomException.在一种情况下,我会得到一个InvalidFormatException所以我需要从那里重新抛出我的 CustomException。 I tried throwing the exception but it's not working as expected我尝试抛出异常,但没有按预期工作

    @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) {
      ...
    }

After doing some research, I found this thread https://github.com/spring-projects/spring-framework/issues/18299 .在做了一些研究之后,我发现了这个线程https://github.com/spring-projects/spring-framework/issues/18299 So it's not possible for now to rethrow the exception in spring boot.所以现在不可能在 spring boot 中重新抛出异常。 Instead we can try alternative strategy, if we have any.相反,我们可以尝试替代策略,如果有的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM