Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!209.197.12.242.MISMATCH!nx01.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!news1.google.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 12:56:02 -0500 Date: Fri, 22 Apr 2011 10:55:59 -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> <87ipu67b41.fsf@merciadriluca-station.MERCIADRILUCA> <87bozy9qek.fsf@merciadriluca-station.MERCIADRILUCA> In-Reply-To: <87bozy9qek.fsf@merciadriluca-station.MERCIADRILUCA> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <_9-dnRwCx6OvXCzQnZ2dnUVZ_sSdnZ2d@earthlink.com> Lines: 41 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 75.8.126.96 X-Trace: sv3-ptUe0OnEGO8+AgSqvyc2EWgbYQknNVWC2RCFCaT2Xi7lAGuEEQrc8k/UtqB1DN+op0NC+qZg/W586/l!tQ7V4hQTSinPUUYVTVmbZ7oYauXlFzQPBMftVBuLzw42r5+x8yDO7T6W6odGlOizKUwJ0sMNld6t!54DKpmCIjzp2rp8b/wblw6LoPna5kVojC7JuM/ZY8r8= 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: 3408 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:588 On 4/22/2011 9:55 AM, Merciadri Luca wrote: ... > Okay. I might switch to exceptions for dealing with user input, > then. I was dubitative because of so many answers about this... For > example, at > , > Mr. Green tells me that exit should be used for application data/user > errors. Surely exiting with System.exit won't give you any stack dump, > but my idea was the following: does the user really need a stack dump > when (s)he knows that he's given bad values to your program? > There are two separate questions: "What should the program as a whole do?" and "What should an input validation method do?" I think there is general agreement that main should not throw an exception on user input error. Instead, the error should be reported, for example by a message on standard output, and the program should terminate with a non-zero status. To produce that, you need to call System.exit() with the desired return code. Returning to the original question, if you have opened and written to an output file, you need to close or flush it before the program terminates to ensure output of any buffered data. You should first use exceptions to get the information about the error to an appropriate level in the program. If you find the error in main, you will not need to do any exception work. If you find the error e.g. in a conversion method, it should report it, probably through an exception, to its caller. Generally, a conversion or validation method should not be responsible for deciding to shut down the program. The simplest way of doing clean-up is, as Stefan Ram recommends, to have a single System.exit call at the end of main, and a returnCode variable. The objective in the rest of the main program is to ensure that the value of returnCode reflects what happened, including being zero if the input was valid and everything worked. Patricia