Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling all listed exceptions but one with Spring's @ExceptionHandler annotation

I am in reference to Spring MVC's @ExceptionHandler annotation.

I would like for my @ExceptionHandler-annotated method to handle all exception but one or two specific exceptions that would be ignored.

Is this possible with the Spring MVC 3.2? Is there any workaround?

like image 777
balteo Avatar asked Jun 26 '13 10:06

balteo


2 Answers

Why not add two handler methods like

@ExceptionHandler(value={Exception.class})
public ModelAndView all(){
    return new ModelAndView();//return general M&V
}

@ExceptionHandler(value={Ex1.class, Ex2.class})
public ModelAndView special(){
    return new ModelAndView();//return special M&V
}
like image 144
Arun P Johny Avatar answered Sep 18 '22 15:09

Arun P Johny


I don't think there is a way to do that with the @ExceptionHandler annotation.

One approach that should work is for you to provide an implementation of the HandlerExceptionResolver interface. In your implementation you could provide the code from your @ExceptionHandler methods and only execute it for the Exceptions that you wish to handle.

I think for Spring MVC to pick-up your custom HandlerExceptionResolver, it will just need to be registered as a bean within your ApplicationContext.

like image 26
Rob Blake Avatar answered Sep 19 '22 15:09

Rob Blake