title: Is Try-Catch More or Less Expensive Than Try-With-Resources
body: <h1>Question</h1>
  
  <p>I've just started getting back into Java recently and never had an opportunity to use <code>try-with-resources</code>. On the surface it looks great as it can cut down on code, but under the hood is it more or less expensive of an operation than the traditional <code>try-catch</code>? I know <code>try-catch</code> already is an expensive operation, hence my curiosity.</p>
  
  <p>I gave both types a simple test and didn't notice much of a difference at all:</p>
  
  <h1>Test Examples</h1>
  
  <h2>Try-With-Resources Test</h2>
  
  <pre><code>long startTime = System.currentTimeMillis();
  ArrayList&lt;String&gt; list = null;
  
  try (Scanner sc = new Scanner(new File("file.txt"))) {
      list = new ArrayList();
      while (sc.hasNext()) {
          list.add(sc.next());
      }
  } catch (Exception ex) {
      System.err.println("Error: " + ex.getMessage());
  } finally {
      long endTime = System.currentTimeMillis();
      System.out.println("The program completed in " + (endTime - startTime) + " ms");
  }
  </code></pre>
  
  <h2>Traditional Try-Catch Test</h2>
  
  <pre><code>long startTime = System.currentTimeMillis();
  ArrayList&lt;String&gt; list = null;
  Scanner sc = null;
  
  try {
      sc = new Scanner(new File("file.txt"));
      list = new ArrayList();
      while (sc.hasNext()) {
          list.add(sc.next());
      }
  } catch (Exception ex) {
      System.err.println("Error: " + ex.getMessage());
  } finally {
      sc.close();
      long endTime = System.currentTimeMillis();
      System.out.println("The program completed in " + (endTime - startTime) + " ms");
  }
  </code></pre>
  
  <h2>Results</h2>
  
  <p>Both resulted in a time of 15-16ms - no real noticeable difference at all. But admittedly this is a very small test example.</p>
  
  <p>My question again: Under the hood is <code>try-with-resources</code> more or less expensive than a traditional <code>try-catch</code>?</p>