Try-with-resources code

Report a typo

Choose only try-with-resources code:

a)

Reader reader = null;

try {
    reader = new FileReader("file.txt");
    // some code
} finally {
    reader.close();
}

b)

try (Reader reader1 = new FileReader("file1.txt"),
     Reader reader2 = new FileReader("file2.txt"),
     Reader reader3 = new FileReader("file3.txt")) {
    // some code
}

c)

Scanner scanner = null;
try {
    scanner = new Scanner(new File("test.txt"));
    // some code
} catch (FileNotFoundException e) {
    e.printStackTrace();
} finally {
    if (scanner != null) {
        scanner.close();
    }
}

d)

try {
    Reader reader = new FileReader("file.txt");
    try (reader) {
        // some code
    } catch (IOException e) {
        // exception handling
    }
} catch (FileNotFoundException e) {
    // exception handling
}

e)

try (Scanner scanner1 = new Scanner(new File("test1.txt"));
     Scanner scanner2 = new Scanner(new File("test2.txt"))) {
        // some code
} catch (IOException e) {
    // exception handling
}
Select one or more options from the list
___

Create a free account to access the full topic