Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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....

like image 297
lisak Avatar asked Nov 19 '10 00:11

lisak


People also ask

How does Spring controller handle exceptions?

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.

How does Spring handle global exceptions?

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.

How many ways can you handle exceptions in Spring MVC?

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.

Which exception class is related to all the exceptions that are thrown in Spring applications?

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.


1 Answers

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  }) 
like image 83
Arsal Avatar answered Sep 20 '22 21:09

Arsal