springmvc异常
第一种方式。
使用 @ControllerAdvice+ @ ExceptionHandler 注解进行全局异常捕捉。
之前 @ ExceptionHandler不能实现全局异常处理,但是加上@ControllerAdvice就可以实现了。
@ExceptionHandler(myexception.class) //这里也可以是 Exception.class public String ex(Exception e) { System.out.println(e.getMessage()); System.out.println("出错啦!!1"); return null; }
第二种方式。
定义一个类实现HandlerExceptionResolver。
当一个异常发生时,必定会实现类中的重载的方法resolveException
// 全局异常处理 //所有发生的异常都会调用此方法 @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { System.out.println("出现异常啦!!!!"); return null; }