I am wondering how to bind exception handling method to url mapping method:
@Controller
public class UserController {
@Autowired
UserDao userDao;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String users(@ModelAttribute("model") ModelMap model) {
model.addAttribute("userList", userDao.getAll());
String[] b = new String[0];
String a = b[1];
return "user";
}
@ExceptionHandler(Exception.class)
public String handleAllException(Exception ex, @ModelAttribute("model") ModelMap model) {
model.addAttribute("error", "Exception happened");
return "error_screen";
}
}
I intentionally provoke java.lang.ArrayIndexOutOfBoundsException in users method. But I see that handleAllException method wasn't executed.
Question:
What have I forgotten to get done to make Exception Handling work appropriately?
@ModelAttributeincorrectly. Please try something like that: ` @ExceptionHandler(Exception.class) public ModelAndView handleError(Exception exception) { //Code here return new ModelAndView("error"); }`