Java OCA OCP Practice Question 1783

Question

Which statement about the following application is correct?

package mypkg;//w w  w .j a v a  2  s  .  co  m
import java.io.*;

class Exception1 extends RuntimeException {}

public class Main {
   public static void main(String[] argv) throws Exception {  // w1
      try {
         throw new IOException("Auto-pilot error");
      } catch (Exception | Exception1 e) {  // w2
         throw e;
      } catch (Exception a) {  // w3
         throw a;
      }
   }
}
  • A. The code does not compile because of line w1.
  • B. The code does not compile because of line w2.
  • C. The code does not compile because of line w3.
  • D. The code compiles and runs without issue.


B.

Note

A multi-catch block cannot contain two exceptions in which one is a subclass of the other, since it is a redundant expression.

Since Exception1 is a subclass of RuntimeException and RuntimeException is a subclass of Exception, line w2 contains a compilation error, making Option B the correct answer.

The rest of the lines of code do not contain any compilation errors.




PreviousNext

Related