0
\$\begingroup\$

Is it a bad thing to do multiple file closings in single finally block?

Relevant excerpt:

BufferedInputStream inStream;
BufferedOutputStream destStream;
try {
 inStream = new BufferedInputStream(/*...*/);
 destStream = new BufferedOutputStream(/*...*/);
 // ... reading from file
 // ... processing
 // ... writting to file
 destStream.flush();
} catch (Exception e) {
 Log("ERROR: " + e.getMessage());
} finally {
 try { destStream.close(); } catch (Exception ignored) {}
 try { inStream.close(); } catch (Exception ignored) {}
}

EDIT-1: I cant have java7 (so I can't use try-with from java7) :(

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Mar 9, 2017 at 7:47
\$\endgroup\$
2
  • \$\begingroup\$ Java 6 is no longer being updated publicly. I don't suppose you enjoy paying for Oracle Premium Support for Java 6? Or are you using IBM Java 6? \$\endgroup\$ Commented Mar 9, 2017 at 8:52
  • \$\begingroup\$ "Enjoy" is a word not related to my work. (changing tools used in organization is hard task, unless you own "gartner.com" email) \$\endgroup\$ Commented Mar 9, 2017 at 13:04

3 Answers 3

2
\$\begingroup\$

As Nolequen wrote, his option is better to use. But it's only possible in Java 7+

If you want to proceed with your solution you can create one try and put close inside.

 try {
 BufferedInputStream inStream = new BufferedInputStream(/*...*/);
 BufferedOutputStream destStream = new BufferedOutputStream(/*...*/);
 // ... reading from file
 // ... processing
 // ... writting to file
 destStream.flush();
 } catch (Exception e) {
 Log("ERROR: " + e.getMessage());
 } finally {
 try { 
 destStream.close(); 
 inStream.close();
 } catch (Exception ignored) {}
}
Phrancis
20.5k6 gold badges69 silver badges155 bronze badges
answered Mar 9, 2017 at 8:01
\$\endgroup\$
1
\$\begingroup\$

It is better to use try-with-resource statement. So you will not need to close streams manualy:

try (final BufferedInputStream inStream = new BufferedInputStream(/*...*/);
 final BufferedOutputStream destStream = new BufferedOutputStream(/*...*/)) {
 //do some work
} catch (Exception e) {
 Log("ERROR: " + e.getMessage());
}
answered Mar 9, 2017 at 7:53
\$\endgroup\$
2
  • 1
    \$\begingroup\$ That definitely seems better in many ways, but it is java7 only. Right? \$\endgroup\$ Commented Mar 9, 2017 at 7:56
  • \$\begingroup\$ Yes, you right. Prior to Java 7, you can use a finally block with multiple close(). \$\endgroup\$ Commented Mar 9, 2017 at 15:26
1
\$\begingroup\$

Both, InputStream and OutputStream implement the Closable interface in Java6 so you can write your own exception ingnoring closer object/method:

private void closeIgnoringException(Closable closable){
 try { closable.close(); } 
 catch (Exception ignored) {
 ignored.printStacktrace();
 }
}
Set<Closable> closables = new HasSet<Closable>(); 
try {
 BufferedInputStream inStream = new BufferedInputStream(/*...*/);
 closables.add(inStream);
 BufferedOutputStream destStream = new BufferedOutputStream(/*...*/);
 closables.add(destStream );
 // ... reading from file
 // ... processing
 // ... writting to file
 destStream.flush();
} catch (Exception e) {
 Log("ERROR: " + e.getMessage());
} finally {
 for(Closable closable : closables)
 closeIgnoringException(closable );
}
answered Mar 9, 2017 at 9:29
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.