Article View: comp.lang.java.programmer
Article #164Re: Problems with blocking socket
From: Jon Skeet
Date: Mon, 11 Feb 2002 17:16
Date: Mon, 11 Feb 2002 17:16
37 lines
2428 bytes
2428 bytes
Timo Kinnunen <timo.kinnunen@bigfoot.com> wrote:
> Jon Skeet <skeet@pobox.com> wrote:
>
> > Rosinger <rosinger@optushome.com.au> wrote:
> >> System.out.println (response);
> >>
> >> in.close();
> >> out.close();
> >> socket.close();
> >> }
> >> }
> >
> > These close calls should be part of a finally block, so they get
> > executed even if an IO exception is thrown.
>
> What is the best way to close multiple streams? This ofcourse depends
> on whether the method has been declared to throw an IOException. I've
> considered a lot of different approaches, but I haven't found a winner
> yet. Some of the better:
>
> Doesn't throw an IOException:
>
> try {
> // ...
> } finally {
> closeIfNonNullAndIgnoreIOException(in);
> closeIfNonNullAndIgnoreIOException(out);
> closeIfNonNullAndIgnoreIOException(socket);
> }
That's the kind of thing I normally use - IOUtil.close in my case. I
normally make it log any throwables caught, basically.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Technical Details
Message-ID:
<MPG.16d20cbec56a3bcd98aa5e@dnews.peramon.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!t-online.de!newsfeed.wirehub.nl!bnewspeer00.bru.ops.eu.uu.net!bnewsifeed00.bru.ops.eu.uu.net!lnewspost00.lnd.ops.eu.uu.net!emea.uu.net!not-for-mailReferences:
<3C67B406.1070407@optushome.com.au> <MPG.16d1cd1ffbee75e098aa59@dnews.peramon.com> <Xns91B2B43D4BB72timokinnunenbigfootc@127.0.0.1>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,610ca5e9d6f87bb8 X-Google-Attributes: gid10d6db,public X-Google-Thread: 10a146,610ca5e9d6f87bb8 X-Google-Attributes: gid10a146,public X-Google-ArrivalTime: 2002-02-11 09:16:31 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!t-online.de!newsfeed.wirehub.nl!bnewspeer00.bru.ops.eu.uu.net!bnewsifeed00.bru.ops.eu.uu.net!lnewspost00.lnd.ops.eu.uu.net!emea.uu.net!not-for-mail From: Jon Skeet <skeet@pobox.com> Newsgroups: comp.lang.java.programmer,aus.computers.java Subject: Re: Problems with blocking socket Date: Mon, 11 Feb 2002 17:16:25 -0000 Message-ID: <MPG.16d20cbec56a3bcd98aa5e@dnews.peramon.com> References: <3C67B406.1070407@optushome.com.au> <MPG.16d1cd1ffbee75e098aa59@dnews.peramon.com> <Xns91B2B43D4BB72timokinnunenbigfootc@127.0.0.1> Organization: Peramon Technology Ltd. X-Newsreader: MicroPlanet Gravity v2.30 X-Original-NNTP-Posting-Host: 10.1.4.5 X-Original-Trace: 11 Feb 2002 17:16:14 GMT, 10.1.4.5 Lines: 37 NNTP-Posting-Host: fw.peramon.com X-Trace: 1013447792 reading.news.pipex.net 8506 193.132.195.125 X-Complaints-To: abuse@uk.uu.net