SQLException捕获不到的问题

xiaoxiao2025-08-23  112

问题描述:做统一异常处理封装类处理,结果发现一直处理不了SQLException及其子类异常

@ResponseBody @ExceptionHandler(value=SQLException.class) public ResponseModel sqlErrorHandler(SQLException e){ System.out.println("sql error handler"); return new ResponseModel(Code.SQL_ERROR.getCode(), Code.SQL_ERROR.getMessage()); }

很是奇怪,因为之前的确做过catch SQLException异常处理,没理由处理不了啊

继续排查,发现cause的虽然是SQLException 但是最初的异常却是org.springframework.daoDataAccessException的子类,说明是属于spring dao 层处理的异常类,而Spring的dao为了统一处理,屏蔽了与特定技术相关的异常,例如SQLException或HibernateException,抛出的异常是与特定技术无关的org.springframework.dao.DataAccessException类的子类。

回想了一下,之前的项目里面貌似没用注解,因此是自己去捕获处理的SQLException,而现在的代码里面使用的是spring的注解,导致现在无法直接捕获处理SQLException,现在去处理DataAccessException

@ResponseBody @ExceptionHandler(value=DataAccessException.class) public ResponseModel dataAccessErrorHandler(DataAccessException e){ System.out.println("data access error handler"); return new ResponseModel(Code.SQL_ERROR.getCode(), Code.SQL_ERROR.getMessage()); }

参考博客如下:https://blog.csdn.net/HardyCheers/article/details/80920095 

 

转载请注明原文地址: https://www.6miu.com/read-5035188.html

最新回复(0)