1

I am trying to display the exception occurred in the controller on the view layer. For this I have setup a try catch block like:

public String persistUserData( )
{
  try
    {
        //Make DB Call
        // Update DB and get new Data

        model.addAttribute( "updatedData", data );

        throw new Exception("Creating an Exception");

    }
    catch(Exception e)
    {
        model.addAttribute("myException", ex.getClass());

    }

    return "myPage.jsp";
}

In my view I am trying to print it with ${myException}, but its not printing anything. What is going wrong here?

3
  • Are you sure this is the exact code you are testing? ${myException} will call toString() on exception, if it does not have any message, you will get null/empty string. Try: model.addAttribute("myException", e.getClass()); Commented Jun 22, 2011 at 18:22
  • Hi Tomasz, I am going to try your solution. Commented Jun 22, 2011 at 19:00
  • Tomasz, e.getClass fetched nothing either on the front end. Commented Jun 22, 2011 at 19:09

3 Answers 3

3

I am so sorry to tell this, but it works for me...

@Controller
@RequestMapping("/")
public class MyController {

    @RequestMapping
    public String omg(@RequestParam("name") String name, Model model) {
        try {
            model.addAttribute("name", name);
            throw new Exception("OMG!");
        } catch (Exception e) {
            model.addAttribute("myException", e);
        }
        return "/WEB-INF/foo.jsp";
    }

}

And foo.jsp:

Name: ${name}<br/>
Error was: ${myException}

This renders (with default Spring MVC configuration under http://localhost:8080/app/?name=abc):

Name: abc
Error was: java.lang.Exception: OMG!

I swear!

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

1 Comment

Sorry Tomasz, I messed it up. Ajax call was overwriting the JSP content :(
1

You can use this pattern :

   @Controller
    @RequestMapping("/")
    public class MyController {
         //catch any exception 
         @ExceptionHandler(Exception.class)
          public ModelAndView handleMyException(Exception  exception) {
             ModelAndView mv = new ModelAndView("errorPage);
             mv.addObject("message",exception.getMessage());
             return mv;
                  } 

          @RequestMapping(value="/doSomething", method=RequestMethod.GET)
          public ModelAndView doSomething() {
           /doSomething
         throw new Exception("OMG!");
            return mv;
                  } 

    }

Hope it helps.

Comments

0

You should return the model from the function, which would look like

return model;

And model should be from type ModelAndView.

2 Comments

But I am able to access other models declared in this method, whats going wrong with exception one.
The code is still incomplete in the way that you are not showing what happens with the model field. The model field is the important one, it has to be returned by a controller method which is annotaded with RequestMapping.

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.