Exceptions

Q1:
public class ExceptionTest 
{ 
    class TestException extends Exception {} 
    public void runTest() throws TestException {} 
    public void test() /* Point X */ 
    { 
        runTest(); 
    } 
}
At Point X on line 5, which code is necessary to make the code compile?

A No code is necessary.

B throws Exception

C catch ( Exception e )

D throws RuntimeException

ANS:A - No code is necessary.

Option B is correct. This works because it DOES throw an exception if an error occurs. Option A is wrong. If you compile the code as given the compiler will complain: 'unreported exception must be caught or declared to be thrown' The class extends Exception so we are forced to test for exceptions. Option C is wrong. The catch statement belongs in a method body not a method specification. Option D is wrong. TestException is a subclass of Exception therefore the test method, in this example, must throw TestException or some other class further up the Exception tree. Throwing RuntimeException is just not on as this belongs in the java.lang.RuntimeException branch (it is not a superclass of TestException). The compiler complains with the same error as in A above.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.