try-with-resources and null resource

Joe Darcy joe.darcy at oracle.com
Wed Jan 26 10:01:23 PST 2011


On 1/26/2011 7:53 AM, Jesper Öqvist wrote:
> Rémi Forax wrote:
>> On 01/21/2011 07:24 PM, Paul Benedict wrote:
>>
>>> The NPE is more useful than the language silently doing nothing with a
>>> null pointer. While your idea is convenient, I believe it assumes too
>>> much. If anything, perhaps the language shouldn't allow an explicit
>>> null assignment in a try-with-resources block.
>>>
>> Hi Paul,
>> the null assignement is just an example. Perhaps, you prefer this one:
>>
>> 4. try(AutoCloseable c = getAnAutoCloseableThatMayBeNull()) {
>> 5.     // nothing
>> 6. }
>>
>> Rémi
>>
> I am concerned about the consistency between what the programmer expects
> and what the compiler produces.
>
> It seems to me that the following code should be legal, and execute
> without throwing an NPE:
>
> try (Resource r = null) {
>      r = new SomeResource();
> }

This code will be rejected by the compiler since r is implicitly final 
and thus the assignment to r inside the block will be reported as erroneous.

-Joe



More information about the coin-dev mailing list