Java OCA OCP Practice Question 1767

Question

Which expression, when inserted into the blank in the following class, allows the code to compile?

package mypkg;//  w  w w .j ava  2s  . c o  m
import java.io.*;
public class Main {
   class Exception1 extends Exception {}
   public void m() throws RuntimeException {
      try {
         throw new Exception1();
      } catch (                           ) {}
   }
}
  • A. Exception a | RuntimeException f
  • B. IllegalStateException | Exception1 t
  • C. Exception1 | IOException i
  • D. Exception1 | Exception x


B.

Note

Option A does not compile because a multi-catch expression uses a single variable, not two variables.

Option C does not compile because it is not possible to throw this checked IOException in the try block.

Option D does not compile because multi-catch blocks cannot contain two exceptions in which one is a subclass of the other.

If it did, one of the two exceptions would be redundant.

Option B is the correct answer and the only expression that allows the class to compile.

While both exceptions in the multi-catch block inherit from Exception, neither is a subclass of the other.




PreviousNext

Related