Override spring @ExceptionHandler methods

If I have a Spring controller with two SEPARATE methods, one of which is annotated:

@ExceptionHandler(Exception.class)

      

and the other is annotated:

@ExceptionHandler(SubException.class)

      

And my controller is throwing SubException.class

, is it handled by both methods or just @ExceptionHandler(SubException.class)

?

+3


source to share


3 answers


One handler will be called based on the best match.



The exact implementation is in AnnotationMethodHandlerExceptionResolver.findBestExceptionHandlerMethod(Object,Exception)

+4


source


You can create your own annotation class which will be an exception. And after that you need to provide the class annotation instead of the Exception handler.



Please let me know if you have any questions.

0


source


Handlers usually implement Spring s ordered interface so you can determine the order in which the handlers are executed.

see Exception Handling in Spring MVC

0


source







All Articles