Irene
Irene

Reputation: 379

Why exception handler is not catching error in Spring MVC

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();

}

Upvotes: 5

Views: 5116

Answers (2)

Sai Surya Kattamuri
Sai Surya Kattamuri

Reputation: 1086

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

Upvotes: 0

matsev
matsev

Reputation: 33789

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.

Upvotes: 3

Related Questions