Friday, 23 July 2010

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.


Lisak said...

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

kolodrevskiy said...


Did you guys found a good workaround for this?

Rubens Mariuzzo said...


Somaiah said...

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?

careerPleasure said...

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

Stevo Slavić said...

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

See this SO entry.

Wiztech Research said...

WIZTECH Automation, Anna Nagar, Chennai, has earned reputation offering the best automation training in Chennai in the field of industrial automation. Flexible timings, hands-on-experience, 100% practical. The candidates are given enhanced job oriented practical training in all major brands of PLCs (AB, Keyence, ABB, GE-FANUC, OMRON, DELTA, SIEMENS, MITSUBISHI, SCHNEIDER, and MESSUNG)

PLC training in chennai
Automation training in chennai
Best plc training in chennai
PLC SCADA training in chennai
Process automation training in chennai
Final year eee projects in chennai
VLSI training in chennai

Wiztech Research said...

Embedded system training: Wiztech Automation Provides Excellent training in embedded system training in Chennai - IEEE Projects - Mechanical projects in Chennai Wiztech provide 100% practical training, Individual focus, Free Accommodation, Placement for top companies. The study also includes standard microcontrollers such as Intel 8051, PIC, AVR, ARM, ARMCotex, Arduino etc.

Embedded system training in chennai
Embedded Course training in chennai
Matlab training in chennai
Android training in chennai
LabVIEW training in chennai
Arduino training in chennai
Robotics training in chennai
Oracle training in chennai
Final year projects in chennai
Mechanical projects in chennai
ece projects in chennai