Spring @ExceptionHandler 处理多种异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4221064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:12:50  来源:igfitidea点击:

Spring @ExceptionHandler handling multiple kinds of exceptions

springexception

提问by lisak

I can't figure out how to handle more than one kind of exception by @ExceptionHandler.

我不知道如何通过@ExceptionHandler 处理一种以上的异常。

I need to programmatically deal with these exceptions, for this I'd need a shared reference. Is this done via this reference "Exception ex" ? I don't think so, cause the exception is not caught like this, how would I do it then ?

我需要以编程方式处理这些异常,为此我需要一个共享引用。这是通过参考“Exception ex”完成的吗?我不这么认为,因为没有像这样捕获异常,那我该怎么做?

I can't put all the exception references as arguments to the handler method, it wouldn't make sense, it can't be programmatically dealt with. I need a shared reference so that I could use "instanceof" on it or just send it somewhere else as a general "Exception"

我不能把所有的异常引用作为处理程序方法的参数,这是没有意义的,它不能以编程方式处理。我需要一个共享引用,以便我可以在它上面使用“instanceof”,或者只是将它作为一般“异常”发送到其他地方

@ExceptionHandler({DescriptionCstOrderException.class, SpecializationCstOrderException.class, NoUploadFileException.class,
                    DeadLineCstOrderException.class, DocumentCstOrderException.class, CommentCstOrderException.class})
public String handleFormException(Exception ex, ActionRequest actionRequest) {
    logger.error(ex.getMessage());
    SessionErrors.add(actionRequest, ex.getClass().getName());  
    return "mainOrderForm";
  }

Additional question: what if I wanted to handle org.springframework.web.multipart.MaxUploadSizeExceededException, that is not thrown from any method of the handler? Because @ExceptionHandlercatches only exceptions that are thrown from one of the handler methods.

附加问题:如果我想处理org.springframework.web.multipart.MaxUploadSizeExceededException,那不是从处理程序的任何方法抛出的怎么办?因为@ExceptionHandler只捕获从处理程序方法之一抛出的异常。

The exceptionHandlermethod could be placed into some extended parent controller but if one uses only defaultAnnotationHandlerMapping... ?

exceptionHandler方法可以放置到某个扩展的父控制器中,但如果只使用defaultAnnotationHandlerMapping... ?

Appreciate any help, I'm going crazy, this is very frustrating....

感谢任何帮助,我快疯了,这很令人沮丧......

采纳答案by NimChimpsky

The @ExceptionHandler value can be set to an array of Exception types. If an exception is thrown matches one of the types in the list, then the method annotated with the matching @ExceptionHandler will be invoked. If the annotation value is not set then the exception types listed as method arguments are used. See the documentationfor details.

@ExceptionHandler 值可以设置为异常类型的数组。如果抛出的异常与列表中的类型之一匹配,则将调用带有匹配 @ExceptionHandler 注释的方法。如果未设置注释值,则使用作为方法参数列出的异常类型。有关详细信息,请参阅文档

回答by Arsal

The @ExceptionHandlervalue can be set to an array of Exception types.

@ExceptionHandler值可以设置为异常类型的数组。

The implementation of using exception array as mentioned in Spring documentationwill be like:

Spring文档中提到的使用异常数组的实现如下:

@ExceptionHandler({
    NotFoundException.class,
    MissingServletRequestParameterException.class
 })

回答by benw

Your question is rather confusing but your exception handler method will only handle one exception at a time. It will not catch multiple exceptions and then pass both of them into your handleFormException() method. If you need to handle these exception types differently then you should create an exception handler method for each one, specify an argument of that specific Exception type to your method, and then do the appropriate handling. For example:

您的问题相当令人困惑,但您的异常处理程序方法一次只能处理一个异常。它不会捕获多个异常,然后将它们都传递给您的 handleFormException() 方法。如果您需要以不同方式处理这些异常类型,那么您应该为每个异常类型创建一个异常处理程序方法,为您的方法指定该特定异常类型的参数,然后进行适当的处​​理。例如:

@ExceptionHandler(DescriptionCstOrderException.class)
public String handleDescriptionCstOrderException(DescriptionCstOrderException exception, ActionRequest actionRequest) {...}


@ExceptionHandler(SpecializationCstOrderException.class)
public String handleSpecializationCstOrderException(SpecializationCstOrderException exception, ActionRequest actionRequest) {...}

// and so on...

Please refer to the Spring documentation for further information:

有关更多信息,请参阅 Spring 文档:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-exceptionhandler

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-exceptionhandler