spring jdbcTemplate如何捕获异常? [英] spring jdbcTemplate how to catch exception?

查看:180
本文介绍了spring jdbcTemplate如何捕获异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在遇到我确实需要捕捉异常的地方之前,一切都很棒。当我放置

Everything is brilliant until I encounter place where I actually do need to catch exception. When I place

jdbcTemplate.query(something...)

in

try{}

阻止我得到:

 Unreachable catch block for SQLException. This exception is never thrown from the try statement body. 

在这种情况下我该怎么办?

What do I do in this situation?

try{
    personIdReturnedByDb = jdbcTemplate.queryForInt(sql, p.getEmail(),
            p.getName(), p.getSurname(), encPw, dateSql);
}

catch(SQLException sa){


}

谢谢,

推荐答案

那是因为 SQLException ,一个已检查的异常,不会被任何 JdbcTemplate.query(...)方法抛出( javadoc链接)。 Spring将其转换为 DataAccessException 之一,这是更通用的运行时异常系列,以抽象出任何特定的底层数据库实现。

That's because SQLException, a checked exception, is not thrown by the any of the JdbcTemplate.query(...) methods (javadoc link). Spring translates this to one of the DataAccessException, which is more generic family of runtime exceptions, in order to abstract away any specific underlying database implementation.

这篇关于spring jdbcTemplate如何捕获异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆