Spring MVC ExceptionHandler needs to be in same class

Had a frustrating evening (pet project) trying to get Spring 3.0 MVC's annotation based exception handler to work.

All the examples seemed straight forwards:

Just annotate a method in a controller:
import org.springframe...ExceptionHandler;
import org.springframe...ModelAndView;
import org.springframe...Controller;
public class MyController {
public ModelAndView myExceptionHandler(
    SomeException exception){

But it just would never catch my exceptions. Very frustrating.

But in the end a google search to the end of the world where someone mentioned in the comments that the exceptionhandler method have to be be in the same class as the method throwing the exception.

Very odd restriction compared to Spring's usual very agile and generic annotation.

So I added my handlers to my abstract controller class that all my controllers extend and problem solved, with some hair still intact.


Comments below were made on a legacy Blog, before move to current Blog (February 2019)


Hi Ivar, do you know how would I do the same If I hadn't extend any abstract controller but I was using only DefaultAnnotationHandlerMapping ? I'm going crazy about this, I got stuck for two hours trying to figure that out ...

19 Nov 2010, 00:42:00
comment author

Hey, Did you guys found a good workaround for this

8 Dec 2011, 22:57:00
comment author
Rubens Mariuzzo:


10 Jul 2012, 19:37:00

Wouldn't it be a better solution be to create a generic exception handler to handle all exceptions using Spring's default exception handler, and then override exception handlers specific to controllers using the annotation?

25 Oct 2012, 14:18:00

I have the same problem. Can you guys give some example programs of how to fix it.

26 Oct 2012, 16:00:00
comment author
Stevo Slavić:

As of Spring 3.2 @ControllerAdvice is available for shared/common @ExceptionHandler methods.

See this SO entry.

26 Mar 2013, 09:21:00
Creative Commons License

Unless otherwise specified, all content is licensed under Creative Commons by Attribution 3.0 (CC-BY).

Externally linked images and content are not included and are licensed and copyrighted separately.