Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Stanimir Stamenkov Newsgroups: comp.lang.java.help Subject: Re: TeeOutputStream? Date: Sun, 05 Jun 2011 10:44:47 +0300 Organization: A noiseless patient Spider Lines: 41 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 5 Jun 2011 07:44:47 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="VdcnnljfizG2LUdZ5iOA6g"; logging-data="19862"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185XBcHoIrnbNcL1vP+RYF+" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20110531 SeaMonkey/2.1pre In-Reply-To: X-Face: )>>ChyF_H public void close() throws IOException { > try { > os1.close(); > } finally { > os2.close(); > } > } > > This guarantees that both close() methods will get called. If both > methods throw IOExceptions the caller will never see the first one as it > will be hidden. I usually do: boolean success = false; try { os1.close(); success = true; } finally { try { os2.close(); } catch (IOException ex) { if (success) { throw ex; } // Log 'ex' possibly as warning to look at // subsequent error logs for details (about // the "original" exception). } } Yes it looks messy. Java 7 promises to bring improvement in that area with try-with-resources (auto-closeable resources) and exception masking: http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html -- Stanimir