Java OCA OCP Practice Question 3189

Question

What is wrong with the following code?

class MyException extends Exception {
}

public class Main {
  public void foo() {
    try {/*from w ww  .j a  v  a 2 s  .  c  om*/
       bar();
    } finally {
       baz();
    } catch (MyException e) {}
  }

   public void bar() throws MyException {
      throw new MyException();
   }

   public void baz() throws RuntimeException {
      throw new RuntimeException();
   }
}

Select the one correct answer.

  • (a) Since the method foo() does not catch the exception generated by the method baz(), it must declare the RuntimeException in a throws clause.
  • (b) A try block cannot be followed by both a catch and a finally block.
  • (c) An empty catch block is not allowed.
  • (d) A catch block cannot follow a finally block.
  • (e) A finally block must always follow one or more catch blocks.


(d)

Note

A try block must be followed by at least one catch or finally block.

No catch blocks can follow a finally block.

Methods need not declare that they can throw Runtime Exceptions, as these are unchecked exceptions.




PreviousNext

Related