try-with-resources and null resource

Mark Thornton mthornton at optrak.co.uk
Fri Jan 28 01:53:50 PST 2011


On 28/01/2011 09:30, Florian Weimer wrote:
>
> By the way, has anybody else seen this phenomenon in their code base?
>
>    InputStream in = null;
>    try {
>      in = new FileInputStream(path);
>      useFile(in);
>    } finally {
>      if (in != null) {
>        in.close();
>      }
>    }
>
> I'm wondering where this is coming from.
>

In my experience, it arises where you have or might expect to extend to 
cases with more than one resource. You only need one try statement 
instead of a whole nest of them. Once you start doing this for the multi 
resource case I suspect there is a tendency to use the same style for 
single resources as well.

Mark Thornton




More information about the coin-dev mailing list