我正在从Spring MVC控制器编写我的异常处理程序,我有以下代码:
现在我正在编写代码以抛出这些异常:
第一行抛出算术异常:
@ExceptionHandler(NullPointerException.class)
public ModelAndView handleMyException(NullPointerException exception) {
System.out.println(exception.getMessage());
ModelAndView modelAndView = new ModelAndView("/errors/404");
modelAndView.addObject("message", exception.getMessage());
return modelAndView;
}
还有第二个:
@ExceptionHandler(ArithmeticException.class)
public ModelAndView handleException(ArithmeticException exception) {
System.out.println(exception.getMessage());
ModelAndView modelAndView = new ModelAndView("/errors/404");
modelAndView.addObject("message", exception.getMessage());
return modelAndView;
}
现在我正在编写代码以抛出这些异常:
第一行抛出算术异常:
System.out.println(100/0);
它抛出正确的异常,并且exception.getMessage()返回消息“/ by zero”。
现在我正在尝试通过以下方式抛出第二个异常:
String test = null;
System.out.println(test.charAt(2));
出现了异常,但是exception.getMessage()返回null,堆栈跟踪也为null。
有趣的是,在log4j记录器中,我看到了两种异常的完整堆栈跟踪。为什么会这样呢?