title: Resource leak while using try...finally?
body: <p>I was working normally in eclipse when I got bugged by a resource leak warning in both <code>return</code> values inside the <code>try</code> block in this method:</p>
  
  <pre><code>@Override
  public boolean isValid(File file) throws IOException
  {
      BufferedReader reader = null;
      try
      {
          reader = new BufferedReader(new FileReader(file));
          String line;
          while((line = reader.readLine()) != null)
          {
              line = line.trim();
              if(line.isEmpty())
                  continue;
              if(line.startsWith("#") == false)
                  return false;
              if(line.startsWith("#MLProperties"))
                  return true;
          }
      }
      finally
      {
          try{reader.close();}catch(Exception e){}
      }
      return false;
  }
  </code></pre>
  
  <p>I don't understand how it would cause resource leak since I'm declaring the <code>reader</code> variable outside the <code>try</code> scope, adding a resource inside the <code>try</code> block and closing it in a <code>finally</code> block using an other <code>try...catch</code> to ignore exceptions and a <code>NullPointerException</code> if <code>reader</code> is <code>null</code> for some reason...</p>
  
  <p>From what I know, <code>finally</code> blocks are always executed when leaving the <code>try...catch</code> structure, so returning a value inside the <code>try</code> block would still execute the <code>finally</code> block before exiting the method...</p>
  
  <p>This can be easily proved by:</p>
  
  <pre><code>public static String test()
  {
      String x = "a";
      try
      {
          x = "b";
          System.out.println("try block");
          return x;
      }
      finally
      {
          System.out.println("finally block");
      }
  }
  
  public static void main(String[] args)
  {
      System.out.println("calling test()");
      String ret = test();
      System.out.println("test() returned "+ret);
  }
  </code></pre>
  
  <p>It result in:</p>
  
  <pre><code>calling test()
  try block
  finally block
  test() returned b
  </code></pre>
  
  <p>Knowing all this, why is eclipse telling me <code>Resource leak: 'reader' is not closed at this location</code> if I'm closing it in my <code>finally</code> block?</p>
  
  <hr>
  
  <h1>Answer</h1>
  
  <p>I would just add to <a href="https://stackoverflow.com/a/15253257/804976">this answer</a> that he's correct, if <code>new BufferedReader</code> throws an exception, an instance of <code>FileReader</code> would be open upon destruction by garbage collector because it wouldn't be assigned to any variable and the <code>finally</code> block would not close it because <code>reader</code> would be <code>null</code>.</p>
  
  <p>This is how I fixed this possible leak:</p>
  
  <pre><code>@Override
  public boolean isValid(File file) throws IOException
  {
      FileReader fileReader = null;
      BufferedReader reader = null;
      try
      {
          fileReader = new FileReader(file);
          reader = new BufferedReader(fileReader);
          String line;
          while((line = reader.readLine()) != null)
          {
              line = line.trim();
              if(line.isEmpty())
                  continue;
              if(line.startsWith("#") == false)
                  return false;
              if(line.startsWith("#MLProperties"))
                  return true;
          }
      }
      finally
      {
          try{reader.close();}catch(Exception e){}
          try{fileReader.close();}catch(Exception ee){}
      }
      return false;
  }
  </code></pre>