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....
Spring MVC provides exception handling for your web application to make sure you are sending your own exception page instead of the server-generated exception to the user. The @ExceptionHandler annotation is used to detect certain runtime exceptions and send responses according to the exception.
To handle exceptions in String MVC, we can define a method in controller class and use the annotation @ExceptionHandler on it. Spring configuration will detect this annotation and register the method as exception handler for argument exception class and its subclasses.
Our goal is to not handle exceptions explicitly in Controller methods where possible. They are a cross-cutting concern better handled separately in dedicated code. There are three options: per exception, per controller or globally. http://github.com/paulc4/mvc-exceptions.
all the exceptions thrown by the spring jdbc framework are subclasses of dataaccessexception which is a type of runtimeexception, so you need not handle it explicitly.
The @ExceptionHandler
value can be set to an array of Exception types.
The implementation of using exception array as mentioned in Spring documentation will be like:
@ExceptionHandler({ NotFoundException.class, MissingServletRequestParameterException.class })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With