OCA Java SE 8 Exception - OCA Mock Question Exception 16








Question

What does the output of the following contain?

     2: public static void main(String[] args) { 
     3:   System.out.print("a"); 
     4:   try { 
     5:      System.out.print("b"); 
     6:      throw new IllegalArgumentException(); 
     7:   } catch (IllegalArgumentException e) { 
     8:      System.out.print("c"); 
     9:      throw new RuntimeException("1"); 
     10:   } catch (RuntimeException e) { 
     11:      System.out.print("d"); 
     12:      throw new RuntimeException("2"); 
     13:   } finally { 
     14:      System.out.print("e"); 
     15:      throw new RuntimeException("3"); 
     16:   } 
     17: } 
  1. abce
  2. abde
  3. An exception with the message set to "1"
  4. An exception with the message set to "2"
  5. An exception with the message set to "3"
  6. Nothing; the code does not compile.




Answer



A, E.

Note

The code prints a on line 3 then prints b on line 5.

Line 6 throws an exception that's caught on line 7.

Line 8 prints c, and then line 9 throws another exception.

public class Main {
  public static void main(String[] args) {
    System.out.print("a");
    try {//  w w w .  ja  v a  2  s .c o m
      System.out.print("b");
      throw new IllegalArgumentException();
    } catch (IllegalArgumentException e) {
      System.out.print("c");
      throw new RuntimeException("1");
    } catch (RuntimeException e) {
      System.out.print("d");
      throw new RuntimeException("2");
    } finally {
      System.out.print("e");
      throw new RuntimeException("3");
    }
  }
}