💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] # 控制器 ~~~ @RequestMapping("/testException") public String testException() throws SysException{ System.out.println("testException执行了..."); try { // 模拟异常 int a = 10/0; } catch (Exception e) { // 打印异常信息 e.printStackTrace(); // 抛出自定义异常信息 throw new SysException("查询所有用户出现错误了..."); } return "success"; } ~~~ # 自定义异常类 ~~~ public class SysException extends Exception{ // 存储提示信息的 private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public SysException(String message) { this.message = message; } } ~~~ # 异常处理器 ~~~ public class SysExceptionResolver implements HandlerExceptionResolver{ public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { // 获取到异常对象 SysException e = null; if(ex instanceof SysException){ e = (SysException)ex; }else{ e = new SysException("系统正在维护...."); } // 创建ModelAndView对象 ModelAndView mv = new ModelAndView(); mv.addObject("errorMsg",e.getMessage()); mv.setViewName("error"); return mv; } } ~~~ # 配置 springmvc.xml beans下 ~~~ <!--配置异常处理器--> <bean id="sysExceptionResolver" class="cn.jdxia.exception.SysExceptionResolver"/> ~~~