venerdì 29 marzo 2013

Q95


Given

public class Main {

       public static void main(String[] args) {

             try {

                    doSomething();

             } catch (SpecialException e) {

                    System.out.println(e);

             }

       }

 

       static void doSomething() {

             int[] ages = new int[4];

             ages[4] = 17;

             doSomethingElse();

       }

 

       static void doSomethingElse() {

             throw new SpecialException("Thrown at end of doSomething() method");

       }

}

 

What is the output?

A.
SpecialException: Thrown at end of doSomething() method

B.
Error in thread “main” java.lang.ArrayIndexOutOfBoundseror

C.
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4 at Main.doSomething(Main.java:12)
at Main.main(Main.java:4)

D.
SpecialException: Thrown at end of doSomething() method at Main.doSomethingElse(Main.java:16)
at Main.doSomething(Main.java:13)
at Main.main(Main.java:4)

Risposta : questa domanda non è Chiara. Per compilar eil codice va cambiato in questo modo :

public class Main {

       public static void main(String[] args) throws SpecialException {

             doSomething();

       }

 

       static void doSomething() throws SpecialException {

             int[] ages = new int[4];

             ages[4] = 17;

             doSomethingElse();

       }

 

       static void doSomethingElse() throws SpecialException {

             throw new SpecialException("Thrown at end of doSomething() method");

       }

}

In questo caso si ottiene :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

       at Main.doSomething(Main.java:8)

       at Main.main(Main.java:3)

 

Nessun commento:

Posta un commento