Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #26259
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | markspace <markspace@nospam.nospam> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: unnecessary code in Oracle example? |
| Date | Wed, 09 Oct 2013 12:27:29 -0700 |
| Organization | A noiseless patient Spider |
| Lines | 66 |
| Message-ID | <l34an4$ic1$1@dont-email.me> (permalink) |
| References | <ydnr4bujt72.fsf@shell.xmission.com> <u6h5u.69447$yx6.64233@fx26.iad> <ydneh7ujm7q.fsf@shell.xmission.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Wed, 9 Oct 2013 19:27:33 +0000 (UTC) |
| Injection-Info | mx05.eternal-september.org; posting-host="9185b88a2b5381a82810209530bb8295"; logging-data="18817"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Zhzx9A7COYamBThIHveMbUc2zSBy1agM=" |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 |
| In-Reply-To | <ydneh7ujm7q.fsf@shell.xmission.com> |
| Cancel-Lock | sha1:Iz9iMZEll9r57xEBv6CPEsdtoKQ= |
| Xref | csiph.com comp.lang.java.programmer:26259 |
Show key headers only | View raw
On 10/9/2013 12:08 PM, Jim Janney wrote:
>
> Thanks. But that looks like a good idiom to avoid. I prefer
>
> MyResource r1 = openResource();
> try {
> MyResource r2 = openResource2();
> try {
> // do something
> } finally {
> r2.close();
> }
> } finally {
> r1.close();
> }
>
> Not as pretty syntactically, perhaps, but much clearer semantically.
>
I don't like either one. Why not take advantage of the fact that
resources are specified to be closed in the opposite order that they are
declared in a try-with-resources?
The following demonstrates this by closing the named streams in the
order C-B-A:
package quicktest;
import java.io.IOException;
import java.io.InputStream;
public class TryResourcesTest
{
public static void main( String[] args ) throws Exception
{
try(
MyTestStream a = new MyTestStream( "A" );
MyTestStream b = new MyTestStream( "B" );
MyTestStream c = new MyTestStream( "C" )
) {
System.out.println( a.read()+b.read()+c.read() );
}
}
}
class MyTestStream extends InputStream {
private final String name;
public MyTestStream( String name ) {
this.name = name;
}
@Override
public int read() throws IOException {
return 42;
}
@Override
public void close() {
System.out.println( getClass().getSimpleName()+
":"+name+" closed." );
}
}
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 10:38 -0600
Re: unnecessary code in Oracle example? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-10-09 11:19 -0700
Re: unnecessary code in Oracle example? ram@zedat.fu-berlin.de (Stefan Ram) - 2013-10-09 19:04 +0000
Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 13:08 -0600
Re: unnecessary code in Oracle example? markspace <markspace@nospam.nospam> - 2013-10-09 12:27 -0700
Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 14:21 -0600
Re: unnecessary code in Oracle example? markspace <markspace@nospam.nospam> - 2013-10-09 14:56 -0700
Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 20:56 -0600
Re: unnecessary code in Oracle example? Arivald <NOSPAMarivald@interia.pl> - 2013-10-10 06:42 +0200
Re: unnecessary code in Oracle example? Sebastian <news@seyweiler.dyndns.org> - 2013-10-10 07:47 +0200
Re: unnecessary code in Oracle example? Arivald <arivald_@AT_interia_DOT_.pl> - 2013-10-10 10:26 +0200
Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-10 08:47 -0600
Re: unnecessary code in Oracle example? Robert Klemme <shortcutter@googlemail.com> - 2013-10-09 21:11 +0200
Re: unnecessary code in Oracle example? cuzdav@gmail.com - 2013-10-10 08:49 -0700
csiph-web