What is the result, if the file myfile.txt does not exist?

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?

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.



Leave a Reply 8

Your email address will not be published. Required fields are marked *


Kamil

Kamil

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

Boris

Boris

I agree; Answer is D

Tim

Tim

D

Tim

Tim

If catch problem is fixed, then A.

Rudi

Rudi

D. Compilation fails

The exception FileNotFoundException is already caught by the alternative IOException