Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #26252

unnecessary code in Oracle example?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Jim Janney <jjanney@shell.xmission.com>
Newsgroups comp.lang.java.programmer
Subject unnecessary code in Oracle example?
Date Wed, 09 Oct 2013 10:38:09 -0600
Organization o gerbil libre go
Lines 36
Message-ID <ydnr4bujt72.fsf@shell.xmission.com> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info mx05.eternal-september.org; posting-host="c75a2e3068ccf6c98395286f3e40b97d"; logging-data="16240"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5u/HaABr35hUruIlWGMN1"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)
Cancel-Lock sha1:51I43/N3dcagsbvABhjlPBH4dbA= sha1:xjCOBxM6lwdDsjtc8JENN2Jhp7o=
Xref csiph.com comp.lang.java.programmer:26252

Show key headers only | View raw


I'm reading up on Java 7's try-with-resource statement and looking at
the tutorial at

http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

which includes the following code: 

> static String readFirstLineFromFileWithFinallyBlock(String path)
>                                                    throws IOException {
>    BufferedReader br = new BufferedReader(new FileReader(path));
>    try {
>        return br.readLine();
>    } finally {
>        if (br != null) br.close();
>    }
> }

As a matter of habit, I always write that pattern as 


> static String readFirstLineFromFileWithFinallyBlock(String path)
>                                                     throws IOException {
>    BufferedReader br = new BufferedReader(new FileReader(path));
>    try {
>        return br.readLine();
>    } finally {
>        br.close();
>    }
> }

on the theory that if you reach that point br can never be null, so the
test is both redundant and confusing.  On the other hand, I might be
wrong.  Is there a reason to test for null in the finally block?

-- 
Jim Janney

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 10:38 -0600
  Re: unnecessary code in Oracle example? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-10-09 11:19 -0700
    Re: unnecessary code in Oracle example? ram@zedat.fu-berlin.de (Stefan Ram) - 2013-10-09 19:04 +0000
    Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 13:08 -0600
      Re: unnecessary code in Oracle example? markspace <markspace@nospam.nospam> - 2013-10-09 12:27 -0700
        Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 14:21 -0600
          Re: unnecessary code in Oracle example? markspace <markspace@nospam.nospam> - 2013-10-09 14:56 -0700
            Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 20:56 -0600
              Re: unnecessary code in Oracle example? Arivald <NOSPAMarivald@interia.pl> - 2013-10-10 06:42 +0200
                Re: unnecessary code in Oracle example? Sebastian <news@seyweiler.dyndns.org> - 2013-10-10 07:47 +0200
                Re: unnecessary code in Oracle example? Arivald <arivald_@AT_interia_DOT_.pl> - 2013-10-10 10:26 +0200
                Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-10 08:47 -0600
    Re: unnecessary code in Oracle example? Robert Klemme <shortcutter@googlemail.com> - 2013-10-09 21:11 +0200
      Re: unnecessary code in Oracle example? cuzdav@gmail.com - 2013-10-10 08:49 -0700

csiph-web