try-with-resources and null resource (original) (raw)

Jesper Öqvist jesper.oqvist at cs.lth.se
Wed Jan 26 07:53:19 PST 2011


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