OCA Java SE 8 Exception - OCA Mock Question Exception 12








Question

Which of the following can be inserted on line 2 to make this code compile? (Choose all that apply)

      1: public void ohNo() throws IOException { 
      2:   // INSERT CODE HERE 
      3: } 
  1. System.out.println("it's ok");
  2. throw new Exception();
  3. throw new IllegalArgumentException();
  4. throw new java.io.IOException();
  5. throw new RuntimeException();




Answer



A, C, D, E.

Note

A method that declares an exception isn't required to throw one so A is correct.

Runtime exceptions can be thrown in any method, making options C and E correct.

D matches the exception type declared and so is also correct.

B is incorrect because a broader exception is not allowed.