【发布时间】:2020-01-28 08:50:40
【问题描述】:
我在 SpringBoot 中创建了一个自定义异常处理程序
@RestControllerAdvice
public class DataApiExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(NoSuchElementException.class)
public final void **handleNoSuchElementException**(NoSuchElementException ex) {
System.err.println("This is throwing :"+ex.getMessage());
}
...
@ExceptionHandler({ Exception.class })
public ResponseEntity<Object> **handleAll**(final Exception ex) {
...
和 抛出异常,如
throw new NoSuchElementException("SomelogicalDescription");
但是每次我抛出这个 NoSuchElementException 时,都会执行 handleAll 而不是 handleNoSuchElementException。
我可能遗漏了一些非常微不足道的东西。帮我找出来。
***To change NoSuchElementException with NotFoundException does not make any difference.***
【问题讨论】:
-
不,我没有,我现在正在这样做。谢谢
-
只需删除(仅用于验证)handleAll**() all 然后检查是否调用 handleNoSuchElementException**()
标签: java spring exceptionhandler