/**
* SpringBoot处理异常方式二:@ExceptionHandler
*/
@Controller
public class DemoController {
@RequestMapping("/show")
public String showInfo(Model modle) {
System.out.println(3/0);
return "index";
}
/**
* java.lang.ArithmeticException
* 该方法需要返回一个ModelAndView,目的是可以让我们封装异常信息以及视图的指定
* @return
*/
@ExceptionHandler(value={java.lang.ArithmeticException.class})
public ModelAndView arithmeticExceptionHandle(Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error");
return mv;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误提示页面--ArithmeticException</title>
</head>
<body>
出错了,请与管理员联系……
<span th:text="${error}"></span><hr/>
</body>
</html>
注意:此方式不能夸controller