Why is there no output when otherMethod is called?

Given the code fragment:

public void otherMethod() {

printFile(“”);

}

public void printFile(String file) {

try (FileInputStream fis = new FileInputStream(file)) {

System.out.println (fis.read());

} catch (IOException e) {

printStackTrace();

}

Why is there no output when otherMethod is called?

Given the code fragment:

public void otherMethod() {

printFile(“”);

}

public void printFile(String file) {

try (FileInputStream fis = new FileInputStream(file)) {

System.out.println (fis.read());

} catch (IOException e) {

printStackTrace();

}

Why is there no output when otherMethod is called?

A.
An exception other than IOException is thrown.

B.
Standard error is not mapped to the console.

C.
There is a compilation error.

D.
The exception is suppressed.

Explanation:
The code compiles fine
The line
FileInputStream fis = new FileInputStream(file))
will fail at runtime since file is an empty string.

Note:
public void printStackTrace()
Prints this throwable and its backtrace to the standard error stream.



Leave a Reply 12

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


Boris

Boris

the correct answer, on my opinion is A.
If e.printStackTrace()will be called the method throws java.io.FileNotFoundException: (No such file or directory)

Boris

Boris

my mistake.. sorry.The answer is correctly. Here what I found on docs.oracle.com/javase…#suppressed-exceptions: If an exception is thrown from the try block and one or more exceptions are thrown from the try-with-resources statement, then those exceptions thrown from the try-with-resources statement are suppressed… You can retrieve these suppressed exceptions by calling the Throwable.getSuppressed method from the exception thrown by the try block.
And anyway, I think, the answer A does not avoid ambiguity..

Francesco

Francesco

Your explanation is wrong. The exceptions are suppressed when more exceptions are thrown.
An example is :

if in the try block an exception is thrown then java automatically try to close the resource declared in the try-withresource statement calling the close() method. If calling the close method throws another exception …. so this exception is suppressed. Then when the catch block is executed you can check for suppressed exceptions by callind Throwable.getSuppressed()

Ray

Ray

The correct answer is D
The exception is suppressed.

Ray

Ray

The correct answer is C.

If change printStackTrace() to e.printStackTrace(), then the answer is B.

Elaine

Elaine

Tested A
throws java.io.FileNotFoundException:

import java.io.FileInputStream;
import java.io.IOException;

public class Q148 {
public void otherMethod() {
printFile(“”);

}

public void printFile(String file) {
try (FileInputStream fs = new FileInputStream(file)) {
System.out.println(fs.read());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
new Q148().otherMethod();
}
}

Marian

Marian

so to conclude the correct answer is B

mknet

mknet

C >> printStackTrace(); causes compilation error

Surfo

Surfo

The answer is D.

Dylan

Dylan

C – Compilation error. Should be e.printStackTrace() instead of just printStackTrace().