package com.resolver; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyExceptionResolver implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { ModelAndView modelAndView=new ModelAndView(); if(e instanceof ClassCastException){ modelAndView.addObject("exception","类转换异常"); }else if(e instanceof RuntimeException){ modelAndView.addObject("exception","运行时异常"); } modelAndView.setViewName("error"); return modelAndView; } }