-2
try {
        bufferedReader = new BufferedReader(new FileReader(new File(file,FILENAME)));
        String readLine = bufferedReader.readLine();
        //do stuff

    } catch(Exception e) {
        throw e; 
    } finally {
        if (bufferedReader!=null)
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

will the bufferedReader closing be invoked in any case in this code?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • 4
    Did you try to debug it? – Reimeus Aug 07 '13 at 18:23
  • @Reimeus yes, but i cant think of every possible test case so i tried to get an answer from someone with better experience... – Ofek Ron Aug 07 '13 at 18:25
  • @OfekRon you could easily supply a file that doesn't exists... – jsedano Aug 07 '13 at 18:25
  • it's closed but maybe still not what you want: http://illegalargumentexception.blogspot.de/2008/10/java-how-not-to-make-mess-of-stream.html (mostly relevant for outputstreams) – zapl Aug 07 '13 at 18:52

5 Answers5

3

yes it invokes in any case( if it is not null in you case).According to java docs

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

If you are using Java7 then I strongly recommond to use try-with-resources Statement.Then you do not require to write the finally block in your code.

try-with-resouce example

     try (BufferedReader  bufferedReader = 
             new BufferedReader(new FileReader(new File(file,FILENAME)));) {
              String readLine = bufferedReader.readLine();
              //do stuff   
      } catch(Exception e) {
           throw e; 
     } 

Note: finally block won't execute in only one case. That is when JVM shutdown(generally with System.exit() statement or when the JVM process is killed externally). In all other cases the finally is guaranteed to execute

StormeHawke
  • 5,987
  • 5
  • 45
  • 73
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
  • 1
    One proviso that is important to note - if the JVM exits before the finally block executes, then it won't execute. ie, `System.exit()` – StormeHawke Aug 07 '13 at 18:26
  • @stormeHawke Yes you are right.That is the only one case finally block won't excutes.But it means JVM shutdown,when JVM shutdown the opened resources will be closed.am I correct..? – Prabhaker A Aug 07 '13 at 18:28
  • @Prabhaker maybe it will be, for the sake of completion, good the add the comment above to the answer... – jsedano Aug 07 '13 at 18:28
  • 1
    @Prabhaker - In this case, most likely, though that is an OS dependent assumption. IE, the OS has to recognize that the JVM has shut down and release any file locks. Not all OSes do this (Windows is fairly notorious for not always catching this). Additionally, any other "cleanup" operations that might be intended to be persistent in some fashion won't get executed. It's probably a rare case where this will cause a problem, but it's still important to know – StormeHawke Aug 07 '13 at 18:35
  • 1
    @stormeHawke I updated the answer to reflect your suggestion.Thank you very much for improving my answer. – Prabhaker A Aug 07 '13 at 18:47
  • 1
    @anakata I updated the answer to reflect your suggestion.Thank you very much for improving my answer. – Prabhaker A Aug 07 '13 at 18:48
0

Finally is always executed, even if the exception is thrown or not. So it will execute the bufferedReader.close(); when bufferedReader is not null

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • Note that if something calls System.exit(), or if the process is terminated externally before the block is reached, the finally block will NOT execute. – StormeHawke Aug 07 '13 at 18:39
0

finally in a try/catch block means that it will happen no matter what. Also, the exception would be better stated as IOException. There is really no reason to have a catch block throw an exception in this situation.

Note: It is true that invoking System.exit() will terminate your could as soon as it is encountered and will result in the application being terminated by the JVM (Java Virtual Machine).

Jeremy Johnson
  • 469
  • 1
  • 4
  • 17
  • Note that if something calls System.exit(), or if the process is terminated externally before the block is reached, the finally block will NOT execute. – StormeHawke Aug 07 '13 at 18:38
  • 1
    @StormeHawke Answer above has been changed to reflect your comment as this is true. – Jeremy Johnson Aug 07 '13 at 18:54
0

I would refer you to this question:

In Java, is the "finally" block guaranteed to be called (in the main method)?

Basically, the finally block will always get run, with one exception: If the JVM exits before the finally block executes.

As to whether the bufferedReader executing - as written, so long as it's not null, yes, with the above noted exception

Community
  • 1
  • 1
StormeHawke
  • 5,987
  • 5
  • 45
  • 73
0

Yes, this code will close bufferedReader in any situation. But in general the code looks like a mess. Java 7 try-with-resources is the best solution of closing resources

try (BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(file,FILENAME))) {
   //do stuff
}
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Try-with-resources is a great thing. Note that if something calls System.exit(), or if the process is terminated externally before the block is reached, the finally block will NOT execute. – StormeHawke Aug 07 '13 at 18:37