一切都很棒,直到遇到我确实需要捕捉异常的地方.当我放置
jdbcTemplate.query(something...)
Run Code Online (Sandbox Code Playgroud)
在
try{}
Run Code Online (Sandbox Code Playgroud)
块我得到:
Unreachable catch block for SQLException. This exception is never thrown from the try statement body.
Run Code Online (Sandbox Code Playgroud)
在这种情况下我该怎么办?
try{
personIdReturnedByDb = jdbcTemplate.queryForInt(sql, p.getEmail(),
p.getName(), p.getSurname(), encPw, dateSql);
}
catch(SQLException sa){
}
Run Code Online (Sandbox Code Playgroud)
谢谢,
mat*_*sev 34
这是因为SQLException,任何JdbcTemplate.query(...)方法(javadoc链接)都不会抛出已检查的异常.Spring将其转换为DataAccessException之一,DataAccessException是更通用的运行时异常系列,用于抽象出任何特定的底层数据库实现.
小智 5
您应该捕获JdbcTemplate异常
即
try
{
// Your Code
}
catch (InvalidResultSetAccessException e)
{
throw new RuntimeException(e);
}
catch (DataAccessException e)
{
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38362 次 |
| 最近记录: |