views:

91

answers:

1

Hello,

I have a method in my controller which will handle the exceptions thrown by the application. So I have a method like this one.

@Controller
public class ExceptionController {

    @RequestMapping(value="/error")
    @ExceptionHandler(value={Exception.class, NullPointerException.class})
    public String showError(Exception e){
        return "tiles:error";
    }
}

And to try I if it works I throw a NullPointerException in another method in other method controller:

boolean a = true;
if(a){ 
    throw new NullPointerException();
}

After the exception is thrown it is printed in the JSP, but it doesn't go throw my showError() method (I've set a breakpoint there and it never enters). showError() method will catch the exception and will show different error pages depending on the exception type (though now it always shows the same error page). If I go to the url /error it shows the error page so the showError() method is OK.

I'm using Spring 3.

What can be the problem?

UPDATE: It caughts only the exceptions thrown in the same controller, but not the others, why?

Thanks.

+2  A: 

If you look at your logs, you'll probably see this:

java.lang.IllegalStateException: Unsupported argument [org.springframework.ui.Model] for @ExceptionHandler method

In other words, @ExceptionHandler methods are not permitted to declare a Model parameter (see docs).

Remove that parameter (which you're not using anyway), and it should work as expected.

skaffman
Thanks it's true, but it doen't solve the problem. I've realized that the exception is only caught when it is thrown by a method which is inside the same controller. It may be because of this bug: https://jira.springsource.org/browse/SPR-5959
Javi
@Javi: That's not a bug, it's by design, and stated in the docs: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-exceptionhandler
skaffman
i looked at the doc page u linked to above, but I must be dense, I'm not seeing anything that says that you can't include a Model parameter in the method, can you point that out?
chrismarx
@chrismarx: The page lists the sort of objects you can get as parameters to the method, and the sort of objects you can return from it. `Model` is in the latter, but not the former.
skaffman
@skaffman i see, much obliged!
chrismarx