Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 22 Apr 2011 07:09:26 -0500 Date: Fri, 22 Apr 2011 05:09:23 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 Newsgroups: comp.lang.java.help Subject: Re: Good practice or not to close the file before System.exit(1)? References: <87mxjjqmlf.fsf@merciadriluca-station.MERCIADRILUCA> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Message-ID: Lines: 28 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 75.8.126.96 X-Trace: sv3-gvAgkCEnGn2bZjqGey6ZeqkcvQaDwp+mLjSoU8iICVmnadnn2iDxX+qjDYqEBF0ZQdK9mTrk1x66AQY!9yTMpPNPWPWfQSjwz/QH12US1OjzJGPWz+yqf6j41JEP0YkSMwKiimBhCCOcR1MlqCdZgbOr7xEO!tPRMMTHPjan76IPcfbolMCtztBHiU4yPBO3NLQ6Pu1Q= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2384 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:577 On 4/22/2011 4:39 AM, rossum wrote: > On 22 Apr 2011 01:57:28 GMT, ram@zedat.fu-berlin.de (Stefan Ram) > wrote: > >> rossum writes: >>> The standard way to do this is by putting the close in the finally >>> block of a try ... finally. Have you covered them in your course yet? >> >> How would this help in >> >> BufferedReader inputFile = new BufferedReader(...); >> try { ... System.exit( 1 ); } >> finally { inputFile.close(); } >> >> ? I do not endorse writing »System.exit« in the middle of a program >> as above, but I though that was what the OP wanted to do. >> > Good catch, my mistake. System.exit() bypasses any finally blocks. > Ouch. > > To the OP: don't use System.exit(), throw an exception instead. If the program should terminate with a non-zero status code, the OP has to call System.exit(). Exceptions can only reduce the number of different places where it is called, and put the calls at an appropriate level in the call hierarchy. Patricia