Article View: borland.public.jbuilder.java.language
Article #7280Exceptions in finally {}
From: Tom Douglas
Date: Fri, 29 Jun 2001 21:10
Date: Fri, 29 Jun 2001 21:10
42 lines
2493 bytes
2493 bytes
Hi, I can't seem to find the answer to this question (perhaps looking in the
wrong places).
I have seen things similar to this code snippet in several examples. However
I'm wondering if the statement in the finally section is incorrect:
//Code snippet
//The source object is not particularly relevant to the question
try {
InputStream in = source.getInputStream();
OutputStream out = source.getOutputStream();
// do input and output
} catch (IOException ioe) {
// handle exception
} finally {
//the following try/catch section is where my question lies
try {
in.close();
out.close();
} catch (IOException ioe1) {};
}
Question 1: if in.close() throws an exception, will out.close() be called?
[I think the answer is no but can't find anything definitive. My thinking is
that if an exception is thrown then the program flow will jump to the catch
section and never return to the out.close() call.]
Question 2: Is the following the "proper" way to handle the finally section
or is this adding too much code?
try {
if (in != null) {
try {
in.close();
} catch (IOException ioe1) {);
if (out != null) {
try {
out.close();
} catch (IOException ioe1) {);
}
Thanks for any help you can provide!
Tom Douglas
Technical Details
Message-ID:
<l96%6.41351$ID1.10988040@typhoon.nyroc.rr.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed1.cidera.com!cyclone.nyroc.rr.com!cyclone-out.nyroc.rr.com!typhoon.nyroc.rr.com.POSTED!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,2dfef93c604f7a74,start
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-06-29 14:12:00 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed1.cidera.com!cyclone.nyroc.rr.com!cyclone-out.nyroc.rr.com!typhoon.nyroc.rr.com.POSTED!not-for-mail
From: "Tom Douglas" <tdouglas@rochester.rr.com>
Newsgroups: borland.public.jbuilder.java.language
Subject: Exceptions in finally {}
Lines: 44
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
Message-ID: <l96%6.41351$ID1.10988040@typhoon.nyroc.rr.com>
Date: Fri, 29 Jun 2001 21:10:41 GMT
NNTP-Posting-Host: 24.161.67.158
X-Complaints-To: abuse@rochester.rr.com
X-Trace: typhoon.nyroc.rr.com 993849041 24.161.67.158 (Fri, 29 Jun 2001 17:10:41 EDT)
NNTP-Posting-Date: Fri, 29 Jun 2001 17:10:41 EDT
Organization: Time Warner Road Runner - Rochester NY