Skip to content

Try With Resources


There is a newer form of the try/catch block. The new way is referred to as Try With Resources. Here's an example of the old way.

BufferedReader input = null;

try {
    input = new BufferedReader(new FileReader("sample.txt"));

    while (input.ready()) {
        System.out.println(input.readLine());
    }
} catch (FileNotFoundException fileNotFound) {
    System.out.println("There was a problem finding the file.");
    fileNotFound.printStackTrace();
} catch (IOException inputOutputException) {
    System.out.println("There was a problem reading the file.");
    inputOutputException.printStackTrace();
} catch (Exception exception) {
    System.out.println("There was a problem.");
    exception.printStackTrace();
} finally {

    try {
        if (input != null) {
            input.close();
        }
    } catch (Exception exception) {
        System.out.println("There was a problem closing the file.");
        exception.printStackTrace();
    }
}

And here is the code that uses the new form.

try (BufferedReader input = new BufferedReader(new FileReader("sample.txt"))) {

    while (input.ready()) {
        System.out.println(input.readLine());
    }
} catch (FileNotFoundException fileNotFound) {
    System.out.println("There was a problem finding the file.");
    fileNotFound.printStackTrace();
} catch (IOException inputOutputException) {
    System.out.println("There was a problem reading the file.");
    inputOutputException.printStackTrace();
} catch (Exception exception) {
    System.out.println("There was a problem.");
    exception.printStackTrace();
}

Syntax

The try statement has a set of parentheses.

  • try (...)

Anything opened inside the parentheses will be closed automatically.

  • try (BufferedReader input = new BufferedReader(new FileReader("sample.txt")))

This done partially for reduced code. That is nice but the most important feature is that this form is guaranteed to close the resources. There are some circumstances where a finally clause can be bypassed. The most common bypass is to have a try block and also a throws clause on a method. The throws will be invoked before the finally and the resource will not be closed.


Multiple Resources

The resources statement can have multiple resources on multiple lines. There needs to be a semicolon at the end of all lines except the last one. Here's an example.

try (
    BufferedReader input = new BufferedReader(new FileReader("sample.txt"));
    PrintWriter output = new PrintWriter(new BufferedWriter(
            new FileWriter("foo.out")))
) {

    while (input.ready()) {
        output.println(input.readLine());
    }
} catch (FileNotFoundException fileNotFound) {
    System.out.println("There was a problem finding the file.");
    fileNotFound.printStackTrace();
} catch (IOException inputOutputException) {
    System.out.println("There was a problem reading the file.");
    inputOutputException.printStackTrace();
} catch (Exception exception) {
    System.out.println("There was a problem.");
    exception.printStackTrace();
}