title: Java io ugly try-finally block
body: <p>Is there a not so ugly way of treat the <code>close()</code> exception to close both streams then:</p>
  
  <pre><code>    InputStream in = new FileInputStream(inputFileName);
      OutputStream out = new FileOutputStream(outputFileName);
  
      try {
          copy(in, out);
      } finally {
          try {
              in.close();
          } catch (Exception e) {
              try {
                  // event if in.close fails, need to close the out
                  out.close();
              } catch (Exception e2) {}
                  throw e; // and throw the 'in' exception
              }
          }
          out.close();
      }
  </code></pre>
  
  <p>update: All the above code is within one more try-catch, thanks for the warnings.</p>
  
  <p><strong>FINALLY (after the answers):</strong></p>
  
  <p>And a good utility method can be done using <a href="https://stackoverflow.com/questions/341971/what-is-the-execute-around-idiom">Execute Around idiom</a> (thanks Tom Hawtin).</p>