try-with-resources and null resource

Vimil Saju vimilsaju at yahoo.com
Mon Jan 31 15:23:01 PST 2011


>--- On Mon, 1/31/11, Rémi Forax <forax at univ-mlv.fr> wrote:
>
>From: Rémi Forax <forax at univ-mlv.fr>
>Subject: Re: try-with-resources and null resource
>To: coin-dev at openjdk.java.net
>Date: Monday, January 31, 2011, 12:30 PM
>
>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);
>}

That looks good, I was a bit worried that a catch block couldn't be attached to the new try-with-resources construct




      



More information about the coin-dev mailing list