1

I am trying to capture all exceptions of some class in my Controller class. It works fine when I define it like this:

@ExceptionHandler(NoSearchResultException.class)
    public String handleNoSearchResultException() {
        return "someView";
    }

But not if I add any parameters:

@ExceptionHandler(NoSearchResultException.class)
    public String handleNoSearchResultException(Exception e) {
        return "someView";
    }

What could possibly be happening? Also, I've read @ExceptionHandler does not support Model arguments, so how would I pass a parameter (like the error message for instance) to the view in order to offer a dynamic error page?

M Rajoy
  • 4,028
  • 14
  • 54
  • 111
  • I think you forgot to add the parameters..... – Paddyd Sep 02 '13 at 11:06
  • Woops, sorry about that. Fixed it. – M Rajoy Sep 02 '13 at 11:09
  • `@ExceptionHandler` **does** support the Model but only as a return value NOT as an argument. So you can put whatever you like in there (you could even return a `ModelAndView` with everything in it, instead of only a view name). More information about supported method argument types and return value types in the [javadoc](http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html). Could you post your mvc configuration and which Spring version are you using. – M. Deinum Sep 02 '13 at 11:20

2 Answers2

3

To pass a parameter to the view I would create a custom Exception class in which you can store any required model parameters (such as error messages). Then in @ExceptionHandler method you can extract those model parameters and make them available in the view. For example:

class RequestException extends RuntimeException {
...
    public void setErrorMessages(List<String> errorMsgs) {
        this.errorMessages = errorMsgs
    }
...
}

@ExceptionHandler(RequestException.class)
public ModelAndView handleNoSearchResultException(RequestException ex) {
    ModelAndView mav = new ModelAndView("someView");
    mav.addObject("errors", ex.getErrorMessages());         //fetch error messages
    return mav;
}

As for parameters, try specifying NoSearchResultException as method parameter instead of it's Exception superclass.

EDIT: Had a bug in 2nd example return value.

Krešimir Nesek
  • 5,302
  • 4
  • 29
  • 56
1

I Solved the problem by passing the custom arguments in request itself.

code is as below :

Controller

@RequestMapping(method = RequestMethod.GET, value = "/exception2")
public String getException1(ModelMap model, @CRequestParam("p") String p, HttpServletRequest request) {

  System.out.println("Exception 2 " + p);
  request.setAttribute("p", p);
  throw new CustomGenericException("1", "2");
}

Exception Handler

@ExceptionHandler(CustomGenericException.class)
public ModelAndView handleCustomException(CustomGenericException ex, HttpServletRequest request) {

  ModelAndView model2 = new ModelAndView("error/generic_error");
  model2.addObject("exception", ex);
  System.out.println(request.getAttribute("p"));
  System.out.println("CustomGenericException  ");
  return model2;

}

here is Sackoverflow question and its answer and

Complete source code is available at git

Community
  • 1
  • 1
Santosh Joshi
  • 3,290
  • 5
  • 36
  • 49