[英]How to add an error to request context rather than throw exception in Spring
How can I set an error in Request Context rather than throw an Exception ? 如何在Request Context中设置错误而不是抛出Exception ?
Like: 喜欢:
void valid(Object o){
if(o == null){
//add an error to Request context
//I want to avoid the throw new ....
return;
}
}
I believe exception is not a functionality to be avoided. 我认为,例外是无法避免的功能。 It was created for a purpose, and should be used in this way.
它是出于特定目的而创建的,应该以这种方式使用。
The problem is when devs use them to help on the application's flow, because exception is an expensive solution for that, and there are other ways for doing so. 问题在于开发人员何时使用它们来帮助应用程序流程,因为异常是解决该问题的昂贵方法,并且还有其他方法可以做到这一点。
There is nothing wrong in throwing a BadRequestException for example when there is some user input failure. 例如,当某些用户输入失败时,抛出BadRequestException并没有错。 Some frameworks are already expecting specific exceptions, and most of them is already prepared for treatment for customized exceptions.
一些框架已经在期待特定的例外,并且大多数框架已经为处理自定义例外做好了准备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.