4

How to handle user defined exceptions (Custom Exception ex.: "BusinessException") in Spring MVC 3 with custom message and view name ?

For Example :

If i throw my own exception from Service layer it should be caught and should redirect to specified view with message, the view name may be same or different.

I searched in Google, but no luck.

Thanks.

3 Answers 3

6

Have you checked @ExceptionHandler

for eg :

@ExceptionHandler(MyBusinessException.class)
public ModelAndView handleMyBusinessException(MyBusinessException e) {
   handle it or log it or redirect to error page after populating a model
}

This has advantage of having Exception handled at Spring MVC level itself , you can populate a model and bring up a meaningful error page.

Otherwise you can configure it in web.xml as other answer suggests. But your error page will be more like a static page.

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

10 Comments

Hi Subin, Thanks for your post. Yes, here we can populate our own model with message and view. But again view name will be tightly coupled. Can you please give an example ? i am not really convinced. Thank you.
just return new ModelAndView("errorviewname") from this method, before that populate dynamic content in ModelMap. It is same like other controller method.View name will be tightly coupled, why would you want to avoid it.Let me know the functionality that you are looking for.,
Ok. I have Controller class which will map 4 requests (4 methods). and from all method "MyBusinessException" is expected. And for all the method the view name will be different if exception is thrown. So if i have one exception handler method in my controller then how can i decide view name ?
easy solution is to set the view name in MyBusinessException object before it is thrown, so in error handler you know the view to forward request to
I can not set view name in "MyBusinessException" object. because it is thrown from Service Layer. and "MyBusinessException" is generic exception for whole service layer.
|
2

You have to propagate exception from service layer to controller layer there you can usedeclarative exception handling and provide exception to view mapping in spring configuration xml

1 Comment

Hi, Thanks you for your post. After some research i think i have found some solution. The Class provided by Spring is "SimpleMappingExceptionResolver" and abstract class "HandlerInterceptorAdapter". Do you know how to implement ? and which one i have to use. I tried a lot but not successful. I am new to "Spring MVC".
0

----SOLVED----

Thanks for all who have helped me to achieve solution for my problem. Finally i got solution for this.

The answer is "ExceptionHandlerExceptionResolver" class from Spring 3.1, By overriding "resolveException" method.

Thank You all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.