Without using the try/catch/finally
, if an exception is thrown, your BufferedReader
won't be closed properly, causing a memory leak, potential unwanted file locking...
Using finally
, your BufferedReader
will be closed, even if an exception is thrown while processing the try
block.
But as your BufferedReader
instantiation can also throw an exception, you should include it in your try
block:
BufferedReader br = null;
try {
br = ...;
// do your stuff
}
catch (Exception e) {
// handle exception
}
finally {
if(br != null)
br.close();
}
If you're using Java 7 or newer, go with the elegant Try-with-resources, as stated by Arnaud.