3.异常处理
SpringMVC:HandlerExceptionResolver接口
该接口的每个实现类都是异常的一种处理方式
a.
ExceptionHandlerExceptionResolver:主要提供了@ExceptionHandler注解,并通过该注解处理异常
//该方法可以捕获本类中抛出的ArithmeticException异常
@ExceptionHandler({ArithmeticException.class,ArrayIndexOutOfBoundsException.class })
public String handlerArithmeticException(Exception e) {
System.out.println(e +"============");
return "error" ;
}
@ExceptionHandler标识的方法的参数必须在异常类型(Throwable或其子类) ,不能包含其他类型的参数
异常处理路径:最短优先
如果有方法抛出一个ArithmeticException异常,而该类中有2个对应的异常处理方法:
@ExceptionHandler({Exception.class })
public ModelAndView handlerArithmeticException2(Exception e) {}
@ExceptionHandler({ArithmeticException.class })
public ModelAndView handlerArithmeticException1(Exception e) {}
则优先级:最短优先。
@ExceptionHandler默认只能捕获当前类中的异常方法。
如果发生异常的方法和处理异常的方法不在同一个类中:@ControllerAdvice
@ControllerAdvice
public class MyExceptionHandler {//不是控制器,仅仅是 用于处理异常的类
@ExceptionHandler({Exception.class })
public ModelAndView handlerArithmeticException2(Exception e) {
ModelAndView mv = new ModelAndView("error");
System.out.println(e +"============"+"该@ControllerAdvice中的异常处理方法,可以处理任何类中的异常");
mv.addObject("er", e) ;
return mv;
}
}
总结:
如果一个方法用于处理异常,并且只处理当前类中的异常:@ExceptionHandler
如果一个方法用于处理异常,并且处理所有类中的异常:类前加@ControllerAdvice、处理异常的方法前加@ExceptionHandler
b.
ResponseStatusExceptionResolver:自定义异常显示页面@ResponseStatus
@ResponseStatus(value=HttpStatus.FORBIDDEN,reason="数组越界222!!!")
public class MyArrayIndexOutofBoundsException extends Exception {//自定义异常
}
@ResponseStatus也可以标志在方法前:
@RequestMapping("testMyException")
public String testMyException(@RequestParam("i") Integer i) throws MyArrayIndexOutofBoundsException {
if(i == 3) {
throw new MyArrayIndexOutofBoundsException();//抛出异常
}
return "success" ;
}
@RequestMapping("testMyException2")
public String testMyException2(@RequestParam("i") Integer i) {
if(i == 3) {
return "redirect:testResponseStatus" ;//跳转到某一个 异常处理方法里
}
return "success" ;
}
@RequestMapping("testExceptionHandler")
public String testExceptionHandler() {
// try {
System.out.println( 1/0 );//
// }catch(ArithmeticException e) e
// }catch(Exception e) e
return "success" ;
}
@RequestMapping("testExceptionHandler2")
public String testExceptionHandler2() {
int[] nums = new int[2];
System.out.println(nums[2]);//ArrayIndexOutOfBoundsException
return "success" ;
}
@RequestMapping("testMyException")
public String testMyException(@RequestParam("i") Integer i) throws MyArrayIndexOutofBoundsException {
if(i == 3) {
throw new MyArrayIndexOutofBoundsException();//抛出异常
}
return "success" ;
}
@RequestMapping("testMyException2")
public String testMyException2(@RequestParam("i") Integer i) {
if(i == 3) {
return "redirect:testResponseStatus" ;//跳转到某一个 异常处理方法里
}
return "success" ;
}
@ResponseStatus(value=HttpStatus.CONFLICT ,reason="测试。。。")
@RequestMapping("testResponseStatus")
public String testResponseStatus() {
return "success" ;
}
// @ExceptionHandler({ArithmeticException.class })
// public ModelAndView handlerArithmeticException1(Exception e) {
// ModelAndView mv = new ModelAndView("error");
// System.out.println(e +"============");
// mv.addObject("er", e) ;
// return mv;
// }
// @ExceptionHandler({Exception.class })
// public ModelAndView handlerArithmeticException2(Exception e) {
// ModelAndView mv = new ModelAndView("error");
// System.out.println(e +"============");
// mv.addObject("er", e) ;
// return mv;
// }
//
// //该方法 可以捕获本类中 抛出的ArithmeticException异常
// @ExceptionHandler({ArithmeticException.class,ArrayIndexOutOfBoundsException.class })
// public ModelAndView handlerArithmeticException(Exception e) {
// ModelAndView mv = new ModelAndView("error");
// System.out.println(e +"============");
// mv.addObject("er", e) ;
// return mv;
// }
//
c.异常处理的实现类:
DefaultHandlerExceptionResolver:SPringMVC在一些常见异常的基础上(300/500/405),新增了一些异常,例如:
*
@see org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
*
@see #handleNoSuchRequestHandlingMethod
*
@see #handleHttpRequestMethodNotSupported:如果springmvc的处理方法限制为post方式,如果实际请求为get,则会触发此异常显示的页面
*
@see #handleHttpMediaTypeNotSupported
*
@see #handleMissingServletRequestParameter
*
@see #handleServletRequestBindingException
*
@see #handleTypeMismatch
*
@see #handleHttpMessageNotReadable
*
@see #handleHttpMessageNotWritable
*
@see #handleMethodArgumentNotValidException
*
@see #handleMissingServletRequestParameter
*
@see #handleMissingServletRequestPartException
*
@see #handleBindException
d.
SimpleMappingExceptionResolver:通过配置来实现异常的处理
<!-- SimpleMappingExceptionResolver:以配置的方式处理异常 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 如果发生异常,异常对象会被保存在exceptionAttribute的value值中;并且会放入request域中;异常变量的默认值是exception-->
<!--<property name="exceptionAttribute" value="exception"></property>-->
<property name="exceptionMappings">
<props>
<!-- 相当于catch(ArithmeticException ex){ 跳转:error } -->
<prop key="java.lang.ArithmeticException">
error
</prop>
<prop key="java.lang.NullPointerException">
error
</prop>
</props>
</property>
</bean>