Spring @ExceptionHandler handling multiple kinds of exceptions

I can't figure out how to handle more than one kind of exception by @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 ?

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"

@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 @ExceptionHandler catches only exceptions that are thrown from one of the handler methods.

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

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

23
задан cristid9 12 November 2019 в 12:50
поделиться