try-with-resources and null resource

Paul Benedict pbenedict at apache.org
Wed Jan 26 08:04:15 PST 2011


Jesper,

I don't see how ignoring null helps the programmer. What does such a syntax
mean? As I see it, try-with-resources is about obtaining a resource, doing
work, and closing it. And since you can't obtain a resource out of a null
reference, I think that is an error. That's why I prefer NPE.

Paul

2011/1/26 Jesper Öqvist <jesper.oqvist at cs.lth.se>

> 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();
> }
>
> However with a null check in the try part of the try-with-resource
> statement, or an NPE, execution might not pass through the statement as the
> programmer expects it to.
>
> Jesper
>



More information about the coin-dev mailing list