I am writing my exception handler from Spring MVC controller and I have the following code:
@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; }
and second:
@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; }
Now I am writing my code to throw this exception:
The first line throws an arithmetic exception:
System.out.println(100/0);
Throws the correct exception, and exception.getMessage () returns a "/ zero" message.
now I am trying to throw the second exception:
String test = null; System.out.println(test.charAt(2));
Excluded, but exception.getMessage () returns null and the stack. Tracing is also zero.
Interestingly, in log4j logger, I see a full stack trace for both exceptions. Why does he behave this way?
source share