Java OCA OCP Practice Question 379

Question

Consider the following code...

class MyException extends Exception  {} 

public class Main{ 
     public void myMethod () throws XXXX{ 
         throw new MyException (); 
     } 
} 

What can replace XXXX?

Select 3 options

  • A. MyException
  • B. Exception
  • C. No throws clause is necessary
  • D. Throwable
  • E. RuntimeException


Correct Options are  : A B D

Note

A. and B. are correct.

Because Exception is a superclass of MyException.

C. is wrong.

It is needed because MyException is a checked exception.

Any exception that extends java.lang.Exception but is not a subclass of java.lang.RuntimeException is a checked exception.

D. is correct. Because Throwable is a super class of Exception.

You can use Throwable as well as Exception as both of them are super classes of MyException.

RuntimeException and its subclasses such as NullPointerException and ArrayIndexOutOfBoundsException, is not a checked exception.

So it cannot cover for MyException which is a checked exception.

You cannot use Error as well because it is not in the hierarchy of MyException.




PreviousNext

Related