try-with-resources and null resource

Jesper Öqvist jesper.oqvist at cs.lth.se
Wed Jan 26 10:15:28 PST 2011


On 2011-01-26 19:01, Joe Darcy wrote:
> 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

I did not know the resource would be implicitly final. An oversight on 
my part.

Thanks for clearing this up!

Jesper




More information about the coin-dev mailing list