0

Here is my code:

public static String readFile()
    {

        BufferedReader br = null;
        String line;
        String dump="";

        try
        {
            br = new BufferedReader(new FileReader("dbDumpTest.txt"));
        }
        catch (FileNotFoundException fnfex)
        {
            System.out.println(fnfex.getMessage());
            System.exit(0);
        }

        try
        {
            while( (line = br.readLine()) != null)
            {
                dump += line + "\r\n";
            }
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage() + " Error reading file");
        }
        finally
        {
            br.close();
        }
        return dump;

So eclipse is complaining about an unhandled IO exception caused by br.close();

Why would this cause an IO exception?

My second question is why eclipse doesn't complain about the following code:

InputStream is = null; 
      InputStreamReader isr = null;
      BufferedReader br = null;

      try{
         // open input stream test.txt for reading purpose.
         is = new FileInputStream("c:/test.txt");

         // create new input stream reader
         isr = new InputStreamReader(is);

         // create new buffered reader
         br = new BufferedReader(isr);

         // releases any system resources associated with reader
         br.close();

         // creates error
         br.read();

      }catch(IOException e){

         // IO error
         System.out.println("The buffered reader is closed");
      }finally{

         // releases any system resources associated
         if(is!=null)
            is.close();
         if(isr!=null)
            isr.close();
         if(br!=null)
            br.close();
      }
   }
}

I'd appreciate it if you kept the explanation in Laymen's terms if possible. Thanks for the help in advance

Patrick
  • 87
  • 1
  • 7
  • 1
    Using [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) would simplify the code significantly. – Mick Mnemonic Jun 17 '15 at 18:38
  • It complains, alright. I suspect the method the second code comes from has a `throws` declaration. Or that there is a `try...catch` around this code. Share the complete method, please. – RealSkeptic Jun 17 '15 at 18:39
  • Unless you're hiding something, the second snippet should have the same issue about unhandled `IOException`. – Sotirios Delimanolis Jun 17 '15 at 18:39

1 Answers1

7

Both code examples should have compiler errors complaining about an unhandled IOException. Eclipse shows these as errors in both code examples for me.

The reason is that the close method throws an IOException, a checked exception, when called in the finally block, which is outside a try block.

The fix is to use a try-with-resources statement, which is available in Java 1.7+. The resources declared are implicitly closed.

try (BufferedReader br = new BufferedReader(new FileReader("dbDumpTest.txt")))
{
   // Your br processing code here
}
catch (IOException e)
{
   // Your handling code here
}
// no finally necessary.

Prior to Java 1.7, you need to wrap the calls to close() in their own try-catch blocks inside the finally block. It's a lot of verbose code to ensure that everything is closed and cleaned up.

finally
{
    try{ if (is != null) is.close(); } catch (IOException ignored) {}
    try{ if (isr != null) isr.close(); } catch (IOException ignored) {}
    try{ if (br != null) br.close(); } catch (IOException ignored) {}
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • So after the code within the try block is executed, the BufferedReader I declared in the try-with-resources statement closes automatically?? – Patrick Jun 17 '15 at 18:59
  • 1
    Yes, that is the point of try-with-resources. From the link I used above: "The try-with-resources statement ensures that each resource is closed at the end of the statement." – rgettman Jun 17 '15 at 19:01