Zeus
Zeus

Reputation: 49

unable to catch exception?

why not able to catch EmptyResultDataAccessException?

method:

public List<User> findUserById(Object id) {
    MapSqlParameterSource paramSource = new MapSqlParameterSource("userid", String.valueOf(id);

    try {
             List<User> userList = myDatasource.query("select * from User where userid=:userid", paramSource, new UserMapper());
             Logger.info("number or records: " + userList.size();
    catch (EmptyResultDataAccessException e) {
             e.printStackTrace();
    }

  return userList;
}

userList.size(); returns 0 but im still not able to catch the EmptyResultDataAccessException. what am I doing wrong ?

Upvotes: 1

Views: 111

Answers (1)

Nathan Hughes
Nathan Hughes

Reputation: 96424

It doesn't throw that exception. EmptyResultDataAccessException is thrown by things like JdbcTemplate#queryForObject that have to return a single row (so the exception is for the case where no rows are found, or where more than one row is found); the exception is listed for queryForObject in the Javadoc. Since the query method (guessing this is JdbcTemplate#query) returns a list there's no problem returning an empty list, and no reason to throw an exception.

Upvotes: 1

Related Questions