我想在SpringMVC3中使用带注释的“@ExceptionHandler”来捕捉“错误”。我可以捕获throwable和任何异常,但是当我尝试使用“Error”时,它没有捕获异常。任何想法为什么?下面的代码演示了这个问题。即使明确抛出错误,为什么错误没有被捕获?
@Controller
@RequestMapping("/test")
public class TestExceptionController {
static final Logger logger = Logger.getLogger(TestExceptionController.class);
@RequestMapping(method = RequestMethod.GET)
public String processData(int intValue) throws InvalidDataException {
if (intValue < 6) {
try {
throw new Exception();
} catch (Exception e) {
throw new InvalidDataException();
}
}
return "test";
}
@ExceptionHandler(InvalidDataException.class)
public ModelMap handleException(InvalidDataException ex) {
logger.debug("exception catched :" + ex);
return new ModelMap();
}
}
上面的代码捕获,但下面的代码不捕获。为什么它没有捕捉到错误?
@Controller
@RequestMapping("/test")
public class TestExceptionController {
static final Logger logger = Logger.getLogger(TestExceptionController.class);
@RequestMapping(method = RequestMethod.GET)
public String processData(int intValue) throws Error{
if (intValue < 6) {
try {
throw new Exception();
} catch (Exception e) {
throw new Error();
}
}
return "test";
}
@ExceptionHandler(Error.class)
public ModelMap handleException(Error ex) {
logger.debug("exception catched :" + ex);
return new ModelMap();
}
}
未来,请编辑您现有的问题,而不是创建一个新的问题。我已经投票结束了原文。 –
您使用的是哪个版本的Spring3? – andyb
这是否回答了您的问题:http://stackoverflow.com/questions/8184593/why-error-is-not-getting-caught-even-when-explicitly-thrown/8212177#8212177 –