简体   繁体   English

如何在Spring中添加错误以请求上下文而不是引发异常

[英]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.

相关问题 如何使用 spring 引导引发自定义异常而不是 500 - How to throw customised exception rather than 500 with spring boot Spring 3-如何在传递其他请求参数时引发异常 - Spring 3 - How to throw an exception when additional request parameters are passed 如何让类变量文件检查打印行而不是抛出异常 - How to get class variable file check to print line rather than throw exception 如何在几秒钟内重试某些方法,并在超时时抛出方法的异常而不是 selenium 中的 TimeoutException? - How to retry some methods several seconds, and when timeout throw the method's exception rather than TimeoutException in selenium? 如何使用RxJava 1.x向调用者抛出异常,而不是处理它? - How to throw an exception up to the caller, rather than handle it, using RxJava 1.x? 无效的Enum值JAXB-转换为null而不是引发异常 - Invalid Enum value JAXB - Converts to null rather than throw exception 自定义注解可以抛出自定义异常,而不是 MethodArgumentNotValidException? - A custom annotation can throw a custom exception, rather than MethodArgumentNotValidException? 如何抛出 Spring @Valid “异常”? - How to throw Spring @Valid “exception”? 在这种情况下如何使用抛出异常? - How can I use throw exception in this context? 如何在此代码中添加“抛出异常”? - How to add “throw exception” to this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM