Return to site

JAVA CERTIFICATION QUESTION: The hierarchy of Java exception handlers

· java

When you have multiple catch statements on a single try, which catch goes first? Which goes last?

Given the code:

public class FooException extends Exception {
  public FooException(String msg) {
    super(msg);
  }
}
public class BarException extends RuntimeException {
  public BarException(Throwable reason) {
    super(reason);
  }
}

public class ExceptionalClass {  
  void doSomethingGood() {
    try {
      doSomethingBad();
      System.out.print("All Good"); // line n1
    } catch(FooException fe) {      // line n2
    } catch(BarException be) {
    } catch(Exception e) {          // line n3
    }
  }
  void doSomethingBad() throws FooException {
    throw new FooException("Something Bad");
  }
}

Which statement is correct? Choose one.

A. Compilation fails because the catch clause in line n2 makes the BarException handler unreachable.

B. Compilation fails because the Exception handler in line n3 is unreachable.

C. Compilation fails because the print statement in line n1 is unreachable.

D. Compilation succeeds.

 

 

 

 

·ꓷ uoᴉʇdo sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ ǝɥꓕ ·uoᴉsnʅɔuoꓛ