Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring jdbcTemplate how to catch exception?

Tags:

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

jdbcTemplate.query(something...)

in

try{}

block I get:

 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){


}

Thank You,

like image 505
Aubergine Avatar asked Jan 18 '12 19:01

Aubergine


2 Answers

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.

like image 135
matsev Avatar answered Sep 17 '22 10:09

matsev


You should catch the JdbcTemplate exception

i.e.

try
{
   // Your Code 
}
catch (InvalidResultSetAccessException e) 
{
    throw new RuntimeException(e);
} 
catch (DataAccessException e)
{
    throw new RuntimeException(e);
}
like image 30
praveen.jakkali Avatar answered Sep 21 '22 10:09

praveen.jakkali