martedì 29 ottobre 2013

Q24

Given the code fragment:

public class Base {
     BufferedReader br;
     String record;
     public void process() throws FileNotFoundException {
          br = new BufferedReader(new FileReader("manual.txt"));
     }
}

public class Derived extends Base {
     // insert code here. Line ***
     public static void main(String[] args) {
          try {
                new Derived().process();
          } catch (Exception e) {
          }
     }
}

Which code fragment inserted at line ***, enables the code to compile?
A.
       public void process() throws FileNotFoundException, IOException {
             super.process();
             while ((record = br.readLine()) != null) {
                    System.out.println(record);
              }
       }

B.
       public void process() throws IOException {
             super.process();
             while ((record = br.readLine()) != null) {
                    System.out.println(record);
              }
       }

C.
       public void process() throws Exception {
             super.process();
             while ((record = br.readLine()) != null) {
                    System.out.println(record);
              }
       }

D.
       public void process() {
             try {
                    super.process();
                    while ((record = br.readLine()) != null) {
                           System.out.println(record);
                    }
             } catch (IOException | FileNotFoundException e) {
             }
       }

E.
       public void process() {
             try {
                    super.process();
                    while ((record = br.readLine()) != null) {
                           System.out.println(record);
                    }
             } catch (IOException e) {
             }
       }

 

Risposta E

A.
Non compila perché l’eccezione non è compatibile con la classe padre:
Exception IOException is not compatible with throws clause in Base.process()

 
B.
Non compila perché l’eccezione non è compatibile con la classe padre:
Exception IOException is not compatible with throws clause in Base.process()

 
C.
Non compila perché l’eccezione non è compatibile con la classe padre:
Exception Exception is not compatible with throws clause in Base.process()

 
D.
Non compila perché l’eccezione FileNotFoundException estende IOException e quindi non è permesso catturarle entrambe in un blocco multicatch.

The exception FileNotFoundException is already caught by the alternative IOException

Nessun commento:

Posta un commento