Spring Boot ControllerAdvice TimeoutException未捕获

问题描述

我在controllerAdvice中有多个ExceptionHandler。一个用于TimeoutException,另一个用于Exception类。

当我抛出新的TimeoutException时,它被捕获在Exception Handler中,而不是在TimeoutException Handler中

下面是代码

@ControllerAdvice
public class CoreControllerAdvice {


     @ExceptionHandler(value = { Exception.class })
     @ResponseBody
     public ResponseEntity<ErrorResponse> handleException(Exception ex) {

        log.info("handleException",ex);
     }

     @ExceptionHandler(value = { TimeoutException.class })
     @ResponseBody
     public ResponseEntity<ErrorResponse> handleTimeoutException(Exception ex) {

        log.info("handleTimeoutException",ex);
     }
}

我将Exception抛出为

throw new TimeoutException("test");

有什么可以帮助的,为什么它没有被TimeoutException Handler捕获

谢谢

解决方法

handleTimeoutException方法中的参数对我来说似乎不正确,而不是 Exception ,它应该是 TimeoutException

 @ExceptionHandler(value = { TimeoutException.class })
 @ResponseBody
 public ResponseEntity<ErrorResponse> handleTimeoutException(TimeoutException ex) {

    log.info("handleTimeoutException",ex);
 }