5

I want to catch an Error in springMVC3 using exception handler. I annotated the exception. I can catch throwable and any exception. But when I tried with Error, It is not catching the exception. Any idea why it is so? The below code catches exceptions

ExceptionHandler(InvalidDataException.class)
public ModelMap handleException(InvalidDataException ex) {
    logger.debug("exception catched  :" + ex);

    return new ModelMap();

}

But the below is not catching;

@ExceptionHandler(Error.class)
public ModelMap handleException(Error ex) {
    logger.debug("exception catched  :" + ex);

    return new ModelMap();

}
Irene
  • 379
  • 1
  • 5
  • 9
  • Which exception do you throw in both cases? – Ralph Nov 18 '11 at 14:57
  • possible duplicate of [Why error is not getting catched ,if I throw from my code](http://stackoverflow.com/questions/8184593/why-error-is-not-getting-catched-if-i-throw-from-my-code) – Dave Newton Nov 18 '11 at 15:21
  • This has just been fixed - please refer to this answer: http://stackoverflow.com/questions/5153132/spring3-exceptionhandler-for-servletrequestbindingexception/36334660#answer-36334660 – Yuriy Nakonechnyy Mar 31 '16 at 13:19

2 Answers2

3

The second example is not working because you are catching an Error, which extends Throwable and not Exception. You will find that the code will work if you change to the '@ExceptionHandler' and the 'handleException()' method to either 'Exception', 'InvalidDataException' or any other exception that is of interest.

matsev
  • 26,664
  • 10
  • 102
  • 138
0

Even I too faced the same problem ,I think @ExceptionHandler can deal with exceptions only not with throwable and errors Refer the link:ExceptionHandler doesn't work with Throwable

Community
  • 1
  • 1