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?

3
  • I think you forgot to add the parameters..... Commented Sep 2, 2013 at 11:06
  • Woops, sorry about that. Fixed it. Commented Sep 2, 2013 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. Could you post your mvc configuration and which Spring version are you using. Commented Sep 2, 2013 at 11:20

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.