spring jdbcTemplate如何捕获异常?
问题描述:
一切都很棒,直到我遇到我真的需要抓住异常的地方。当我把spring jdbcTemplate如何捕获异常?
jdbcTemplate.query(something...)
在
try{}
块,我得到:
Unreachable catch block for SQLException. This exception is never thrown from the try statement body.
我怎么在这种情况下怎么办?
try{
personIdReturnedByDb = jdbcTemplate.queryForInt(sql, p.getEmail(),
p.getName(), p.getSurname(), encPw, dateSql);
}
catch(SQLException sa){
}
谢谢你,
答
这是因为SQLException
,检查异常,不被任何JdbcTemplate.query(...)
方法抛出(javadoc link)春将这种对DataAccessException之一,这是比较通用的家庭运行时异常,以抽象掉任何特定的底层数据库的实现。
答
你应该抓住的JdbcTemplate例外
即
try
{
// Your Code
}
catch (InvalidResultSetAccessException e)
{
throw new RuntimeException(e);
}
catch (DataAccessException e)
{
throw new RuntimeException(e);
}
没有任何SQLExceptions; Spring将它们包装在RuntimeException中;我忘记了根类名。 – 2012-01-18 20:00:27
你应该做出答案。 – hvgotcodes 2012-01-18 20:04:18