繁体   English   中英

Hibernate异常继续由RuntimeException.class而不是HibernateException.class处理

[英]Hibernate exceptions keep getting handled by RuntimeException.class rather than HibernateException.class

我有一个ControllerAdvice设置,有两个异常处理程序; 一个用于任何Hibernate异常(HibernateException.class),一个用于任何其他运行时异常(RuntimeException.class)

@ExceptionHandler(HibernateException.class)
public String handleHibernateException(HibernateException exc, Model theModel) {

    String message = "An error has occured: " + exc.getLocalizedMessage() + "\r\n";

    myLogger.warning(message);

    theModel.addAttribute("exception", message);

    return "testing";
}


@ExceptionHandler(RuntimeException.class)
public String handleRuntimeExceptions(RuntimeException exc, Model theModel) {

    if (exc instanceof HibernateException) {

        return handleHibernateException((HibernateException) exc.getCause(), theModel);
    }   

    String message = "An error has occured: " + exc.getLocalizedMessage() + "\n"
            + exc.getCause().getCause().toString() + "\r\n";
    myLogger.warning(message);

    theModel.addAttribute("exception", message);

    return "testing";

我正在通过搞乱我的查询语句来测试它,这给了我一个QuerySyntaxException; HibernateException的子类。 然而,它仍由我的运行时异常方法处理。

我认为@ExceptionHandler完全匹配了QuerySyntaxException的Exact Exception名称。您可以尝试使用QuerySyntaxException。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM