try-with-resources and null resource

Joe Darcy joe.darcy at oracle.com
Mon Jan 31 16:00:37 PST 2011


Vimil Saju wrote:
>> --- 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
>
>   

Since last July [1], JDK 7 builds with an implementation of 
try-with-resources have been available to download and people were 
invited to use that feature in particular and send in feedback.

-Joe

[1] "Project Coin ARM Implementation," 
http://blogs.sun.com/darcy/entry/project_coin_arm_implementation

[2] "Project Coin: Try out try-with-resources", 
http://blogs.sun.com/darcy/entry/project_coin_try_out_try



More information about the coin-dev mailing list