I'm 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 the second one:
@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'm writing my code in order to throw those exception:
The first line throws Arithmetic exception:
System.out.println(100/0);
It throws correct exception and and exception.getMessage() returns message "/ by zero".
now I'm trying to throw second exception by:
String test = null;
System.out.println(test.charAt(2));
The exception is raised, however exception.getMessage() return null and stack Trace is null as well.
The interesting thing is that in log4j logger I see full stack trace for both exceptions. Why does it behave that way?