0

firstly I know I should be using a try-catch with resources, however I don't currently have the most up to date JDK on my system.

I have the following code below, and am trying to ensure the resource reader is closed using the finally block, however the code below doesn't compile for two reasons. Firstly is that reader may have not been initialized and secondly that close() should be caught within its own try-catch. Dont both of these reasons defeat the object of the initial try-catch block?

I can solve the issue with the finally block close() statement by putting it in its own try-catch. However this still leaves the compile error about reader not being initialized?

I'm presuming I have gone wrong somewhere? Help appreciated!

Cheers,

public Path [] getPaths()
    {
        // Create and initialise ArrayList for paths to be stored in when read 
        // from file.
        ArrayList<Path> pathList = new ArrayList();
        BufferedReader reader;
        try
        {
            // Create new buffered read to read lines from file
            reader = Files.newBufferedReader(importPathFile);
            String line = null;
            int i = 0;
            // for each line from the file, add to the array list
            while((line = reader.readLine()) != null)
            {
                pathList.add(0, Paths.get(line));
                i++;
            }
        }
        catch(IOException e)
        {
            System.out.println("exception: " + e.getMessage());
        }
        finally
        {
            reader.close();
        }


        // Move contents from ArrayList into Path [] and return function.
        Path pathArray [] = new Path[(pathList.size())];
        for(int i = 0; i < pathList.size(); i++)
        {
            pathArray[i] = Paths.get(pathList.get(i).toString());
        }
        return pathArray;
    }
Dave0504
  • 1,057
  • 11
  • 30

2 Answers2

2

There is no other way then initialize your buffer and catch the exception. The compiler is always right.

BufferedReader reader = null;
try {
    // do stuff
} catch(IOException e) {
    // handle 
} finally {
    if(reader != null) {
        try {
            reader.close();
        } catch(IOException e1) {
            // handle or forget about it
        }
    }
}

The method close will always need a try-catch-block since it declares that it could throw an IOException. It doesn't matter if the call is in a finally block or somewhere else. It just needs to be handled. It is a checked exception.

Read must also be initialized just by null. IMHO this is super useless, but that's Java. That is how it works.

Thomas Uhrig
  • 30,811
  • 12
  • 60
  • 80
  • Excatly,seemed like pointless code generation :S Thanks for the help solving the initialization issue. – Dave0504 Jun 18 '14 at 19:04
0

Instead check if reader is null or not and then close it accordingly like below (you should call close() on reader only if it's not null or if it's been already instantiated else you will end up getting null reference exception).

   finally
    {
        if(reader != null)
        {  
          reader.close();
        }
    }
Rahul
  • 76,197
  • 13
  • 71
  • 125