Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.help Subject: Re: Good practice or not to close the file before System.exit(1)? Date: Fri, 22 Apr 2011 08:20:30 -0700 Organization: A noiseless patient Spider Lines: 52 Message-ID: References: <87mxjjqmlf.fsf@merciadriluca-station.MERCIADRILUCA> <87ipu67b41.fsf@merciadriluca-station.MERCIADRILUCA> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 22 Apr 2011 15:20:36 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="FaTfWpXf0ZPavolLoJgYCA"; logging-data="14138"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19BSJw4zsEyEY9QNIVlw0WTid1Nd8t3COk=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 In-Reply-To: <87ipu67b41.fsf@merciadriluca-station.MERCIADRILUCA> Cancel-Lock: sha1:Ic2QNI5Jii6R4/xqJVA6ZS0OXL4= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:582 On 4/22/2011 4:56 AM, Merciadri Luca wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > The fact is that when some specific values of variables are > encountered, the program can't continue. I would have preferred > throwing an exception, but there is apparently no reason to throw an > exception because the variables that need to be checked come directly > from the arguments that the user passed to the JVM: > > java myprogram 1 2 3 Doesn't matter, throw an exception. > > It it thus not an `internal error' when some values are encountered, > because they come from the user. Doesn't matter. Exceptions are their for you to extend and use for your own purposes. They are not limited to a few internal errors but cover all types of exceptional processing. > > As a result, I don't throw an exception when some specific values of > the variables are encountered, but I make a System.err.println("Some > text") followed by a System.exit(1). Compare with the Apache CLI package, which does the grunt work of reading command line arguments for you: parse CommandLine parse(Options options, String[] arguments) throws ParseException They make their own exception, ParseException, and throw it when they encounter unknown flags or other errors. The expectation is that you will catch ParseException, print out a usage line, preform any other final processing (close files), and then exit with status. In your particular case, yes you could almost certainly avoid opening the files first. However in general this follows the same pattern that all other exceptional processing follows: throw an error when you have a problem, let the high level code catch it and deal with the results.