Java OCA OCP Practice Question 1044

Question

What is the result of compiling and running this code?

class MyException extends Throwable{} 
class MyException1 extends MyException{} 
class MyException2 extends MyException{} 
class MyException3 extends MyException2{} 

public class Main{ 
   void myMethod () throws MyException{ 
      throw new MyException3 (); 
    } //from w  ww.ja va 2  s  .  c om
   public static void main (String [] args){ 
      Main et = new Main (); 
      try{ 
         et.myMethod (); 
       } 
      catch (MyException me){ 
         System.out.println ("MyException thrown"); 
       } 
      catch (MyException3 me3){ 
         System.out.println ("MyException3 thrown"); 
       } 
      finally{ 
         System.out.println (" Done"); 
       } 
    } 
} 

Select 1 option

  • A. MyException thrown
  • B. MyException3 thrown
  • C. MyException thrown Done D. MyException3 thrown Done
  • E. It fails to compile


Correct Option is  : E

Note

You can have multiple catch blocks to catch different kinds of exceptions, including exceptions that are subclasses of other exceptions.

The catch clause for more specific exceptions should come before the catch clause for more general exceptions.

Failure to do so results in a compiler error as the more specific exception is unreachable.

catch for MyException3 cannot follow catch for MyException because if MyException3 is thrown, it will be caught by the catch clause for MyException.

There is no way the catch clause for MyException3 can ever execute.

And so it becomes an "unreachable" statement.




PreviousNext

Related