title: What is the Java 7 try-with-resources bytecode equivalent using try-catch-finally?
body: <p>I'm trying to understand how the new <a href="http://download.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html" rel="noreferrer">try-with-resources statement</a> works by recreating it using regular try-catch-finally statements. Given the following test class using Java 7 try-with-resources:</p>
  
  <pre><code>import java.io.IOException;
  import java.util.zip.GZIPOutputStream;
  
  public class TryWithResources {
      public static void main(String[] args) {
          try (GZIPOutputStream gzip = new GZIPOutputStream(System.out)) {
              gzip.write("TEST".getBytes("UTF-8"));
          } catch (IOException ioe) {
              ioe.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>How would you rewrite this class to use try-catch-finally statements which produces exactly the same bytecode as the try-with-resources statement produces? Also, same question when two resources are used, as in the following example:</p>
  
  <pre><code>import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.util.zip.GZIPOutputStream;
  
  public class TryWithResources2 {
      public static void main(String[] args) {
          try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  GZIPOutputStream gzip = new GZIPOutputStream(baos)) {
              gzip.write("TEST".getBytes("UTF-8"));
          } catch (IOException ioe) {
              ioe.printStackTrace();
          }
      }
  }
  </code></pre>