venerdì 13 settembre 2013

Q3

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.

Risposta B.
Infatti quando il programma a run time cerca di aprire lo stream file, essendo vuoto, restituisce una java.io.FileNotFoundException:  (No such file or directory). Il metodo printStackTrace() fa qualcosa che non si sa. Se si tratta, come è probabile di un errore e il vero codice fosse:
  catch (IOException e) {
                    e.printStackTrace();
             }
Anche in questo caso la risposta è B perché la FileNotFoundException non viene catturato dalla gestione della IOException.
Riferimenti:

Nessun commento:

Posta un commento