Can we use Model addAttribute to handle errors in the UI ?

Example Sample Code
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ModelAndView addEmployee(Model model, EmpTO empTO, BindingResult bindingResult){
    try {
    } catch (Exception e) {
        if (e instanceof InvException) {
            model.addAttribute("error", ((InvException) e).getError());
        } else {
            model.addAttribute("error", PresentationUtil.handleApplicationRuntimeError(e));
        return new ModelAndView("emp/save");
    empTO = new EmpTO();
    return new ModelAndView("emp/home");

Open in new window


1. Is the above is the correct approach?
2. Can we store the error code and description in a DB table or hard-coded string and let UI handles the error codes?
3. Is this is a standard industry practice?

Software ProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

1. Is the above is the correct approach?

This is one correct approach.

2. Can we store the error code and description in a DB table or hard-coded string and let UI handles the error codes?

You can. Of course the UI should have some logic to handle each different error code.

3. Is this is a standard industry practice?

It's hard to say if this is the standard. There other ways to do it, which I personally think it is easier and provide more readability. When I need to handle exceptions I usually define an exception handler

 public ModelAndView handleInvException(InvException ex) {
  Map<String, InvException> model = new HashMap<String, InvException>();
  model.put("exception", ex);
  return new ModelAndView("error", model);


Open in new window

But of course all this depends on the use case.
Software ProgrammerAuthor Commented:
I like your way via exception handler. what are things needs to be covered in an exception handler or handling exceptions via controller advice?


1. Do we go for exception handler or controller advice to log the exception so that the error will be logged in application log instead of tomcat log ???
2. What are the benefits of handling via exception handler or controller advice ???
An @ExceptionHandler is an annotation defined in a @ControllerAdvice. The two of them are tied together.

public class ExceptionConfiguration {

    public void handleRuntimeException() {}

    public void handleCustomException() {}


Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Software ProgrammerAuthor Commented:
Can you send me the full code of anywhere where controller advice is implemented???  Template is being shown in many web-pages and couldn't understand the full purpose of it. I couldn't understand how it solves the problem. Any code in detail would be very much appreciated.
Software ProgrammerAuthor Commented:
These links i have already read...I need a code from a project or any code which does the controller advice in detail..real time code or usage or code in any open source for reference to follow .....pattern in detail to follow...
I am afraid I don't have any other code to show you. I take it all projects that use ControllerAdvice use similar snippets of code.
Software ProgrammerAuthor Commented:
When Model and ModelMap needs to be used and what is the difference ? Once we know the difference we can close this.
Both are used to pass data to the view.  They are more or less the same. Model is an interface while ModelMap is an implementation of a Map.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.