Given the code fragment:
public class ReadFile01 {
public static void main(String[] args) {
String fileName = “myfile.txt”;
try (BufferedReader buffIn = // Line 4
new BufferedReader(new FileReader(fileName))) {
String line = “”; int count = 1;
line = buffIn.readLine(); // Line 7
do {
line = buffIn.readLine();
System.out.println(count + “: ” + line);
} while (line != null);
} catch (IOException | FileNotFoundException e) {
System.out.println(“Exception: ” + e.getMessage());
}
}
}
What is the result, if the file myfile.txt does not exist?
A.
A runtime exception is thrown at line 4
B.
A runtime exception is thrown at line 7
C.
Creates a new file and prints no output
D.
Compilation fails
Explanation:
There will be a FileNotFoundException at line 4.
Answer is D:
Compilation fails:
error: Alternatives in a multi-catch statement cannot be related by subclassing
Alternative FileNotFoundException is a subclass of alternative IOException
I agree; Answer is D
D
D
D
If catch problem is fixed, then A.
D. Compilation fails
The exception FileNotFoundException is already caught by the alternative IOException
d