Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.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 15:24:49 -0700 Organization: A noiseless patient Spider Lines: 38 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: 8bit Injection-Date: Fri, 22 Apr 2011 22:24:55 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="FaTfWpXf0ZPavolLoJgYCA"; logging-data="25109"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19JYuALBEHVDuJPebzGzaRRv+gDKaPCBgs=" 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: Cancel-Lock: sha1:xZSecq4Ga3bdCGcSPWANZizS1f8= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:594 On 4/22/2011 9:31 AM, Stefan Ram wrote: > Patricia Shanahan writes: >> To take the extreme case of high level code, what is the main >> method of an application supposed to do when, through >> exceptions or otherwise, it finds out that the program should >> terminate with status code 1? > > (I already answered this before in one of my posts in this thread.) > > One single call of »System.exit« is justified for this purpose at > the very end of »main«: This is what I was thinking too. One way to implement this would be to add some sort of extra value to Exceptions to allow lower level code to pass up an exit status (note: this is getting way beyond what the OP should care about, I'm just extending the idea of an exit status here to include exceptions): public interface ExceptionStatus { int getExitStatus(); } ... public class Main { public static void main( String[] args ) { try { // ...whole program... } catch( Throwable t ) { // ...preform final processing... then: if( t instanceof ExceptionStatus ) { int status = ((ExceptionStatus)t).getExitStatus(); System.exit( status ); } } }