java 嵌套异常_java – Spring MVC中的嵌套异常处理

我收到以下错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException

为了在控制器中处理这个问题,我使用了以下代码:

@ExceptionHandler(NestedServletException.class)

public ModelAndView handleServletErrors(){

System.out.println("Servlet Exception is thrown");

ModelAndView mv = new ModelAndView("error");

mv.addObject("error", "Error encountered while processing reqeust.");

return mv;

}

但是这并没有处理上面抛出的异常.如果我使用NullPointerException类而不是NestedServletException,它可以工作.由于Spring在响应NullPointerException时抛出异常,不应该由上面的代码处理吗?

Annotation for handling exceptions in specific handler classes and/or handler methods.

此注释将允许方法处理由处理程序方法引发的异常,即使用@RequestMapping注释的方法.引用Spring reference:

You can do that with @ExceptionHandler methods. When declared within a controller such methods apply to exceptions raised by @RequestMapping methods of that contoroller (or any of its sub-classes). You can also declare an @ExceptionHandler method within an @ControllerAdvice class in which case it handles exceptions from @RequestMapping methods from many controllers.

由于处理程序抛出的异常是NullPointerException,因此异常处理程序方法将处理该特定异常.它不会处理Spring用来封装servlet异常的泛型NestedServletException.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值