我想使用 @ExceptionHandler 来处理任何 Hibernate 异常。如果异常不是 Hibernate 异常,则运行时错误的 @ExceptionHandler 会处理异常。
问题是,运行时异常总是优先于 Hibernate 异常处理程序。意思是,发生的任何休眠异常都由通用运行时异常处理程序处理。
如何确保 Hibernate 异常由其异常处理程序处理,而不是由运行时异常处理?
'@ExceptionHandler(HibernateException.class)
public String handleHibernateException(HibernateException exc, Model theModel) {
String message = "An error has occured: " + exc.getLocalizedMessage() + "\n" + exc.getCause().toString()
+ "\r\n";
myLogger.warning(message);
theModel.addAttribute("exception", message);
return "testing";
}
// handle any other runtime/unchecked exception and log it
@ExceptionHandler(RuntimeException.class)
public String handleRuntimeExceptions(RuntimeException exc, Model theModel) {
String message = "An error has occured: " + exc.getLocalizedMessage() + "\n" + exc.getCause().toString()
+ "\r\n";
myLogger.warning(message);
theModel.addAttribute("exception", message);
return "testing";
}'
一只萌萌小番薯
相关分类