try-with-resources and null resource

Rémi Forax forax at univ-mlv.fr
Mon Jan 31 12:30:01 PST 2011


On 01/31/2011 08:51 PM, Vimil Saju wrote:
> The following code pattern is present at many places in our code base.
> 	List<T>  records = new ArrayList<T>();	ResultSet rs = null;
> 	try {		rs = executeQuery(st);		records = fetchRecords(rs, returnClass);	}	catch(Exception ex) {		logDBError(ex);		throw new DBException(ErrorCodes.DB_FETCH_ERROR, ex);	}	finally {		freeUp(rs);	}
> 	return records;
> How would the about code look like with the new try-with-resources syntax?

I suppose that freeUp(rs) is equivalent to rs.close().

try(ResultSet rs = executeQuery(st)) {
   return fetchRecords(rs, List.class);
} catch(Exception ex) {
   logDBError(ex);
   throw new DBException(ErrorCodes.DB_FETCH_ERROR, ex);
}

Rémi




More information about the coin-dev mailing list