gpt4 book ai didi

Spring Boot 异常处理程序 : throw an exception

转载 作者:行者123 更新时间:2023-12-03 07:40:13 24 4
gpt4 key购买 nike

使用这个效果很好

@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ExceptionHandler(value = IoTException.class)
public void IoTError() {

}

但是当我尝试转换为另一个自制异常时

@ExceptionHandler(value = IoTException.class)
public void IoTError() {
throw new IoTConnectionException();
}

异常处理程序被忽略,即 IoTException 被发送到 View 而不被转换为 IoTConnectionException。但是放置断点显示我进入了 IoTError 方法。知道为什么吗?谢谢:)

最佳答案

docs about exception handling状态:

If an exception occurs during request mapping or is thrown from a request handler such as an @Controller, the DispatcherServlet delegates to a chain of HandlerExceptionResolver beans to resolve the exception and provide alternative handling, which typically is an error response.

在您抛出 IoT 异常的那一刻,对 HandlerExceptionResolver 链的委托(delegate)已经发生,不会再次执行。如果此异常会触发另一个异常处理分派(dispatch),则可能会导致异常循环。想象一下,您会为 IoTConnectionException 定义另一个异常处理程序方法,这会抛出 IoTException。您将以 StackOverflowException 结束。

本节Docs Exception handler methods描述了异常处理程序方法的所有支持的返回值。

关于Spring Boot 异常处理程序 : throw an exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49728492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com