Menu
Topics Index
...
`

Exceptions Test


If you need explanation Read this topic

If you need Answer Take test on this topic

Exceptions Test
Given that the exceptions AEx, BEx and CEx are defined as shown below, which of the methods in RuntimeExceptions fails to compile.
class AEx extends RuntimeException {}
class BEx extends Exception {}
class CEx extends AEx {}

class RuntimeExceptions {

    void m1() throws AEx, BEx, CEx { }

    void m2() throws Exception { throwB(true); throwAC(false); }

    void m3() { throwAC(false); }

    void m4() throws AEx, BEx { throwAC(true); }

    void m5(boolean value) {
        try {
            throwB(value);
        } catch (Exception ex) {
        } finally {
            throwAC(false);
        }
    }

    void throwB(boolean value) throws CEx, BEx {
        if (!value) throw new BEx();
    }

    void throwAC(boolean value) {
        if (!value) throw new AEx();
        throw new CEx();
    }
}

A. m1, m5
B. m1, m2, m3, m4, m5
C. m2, m4, m5
D. m3, m5
E. All of them compile with out any errors
Topic: Exception Handling Syntax In Java Programming

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is e...every one will compile because the rule is that it is ok to not catch the methods that can throwunchecked exceptions....and we should catch or declare throws when method is likely to throw checked exceptions..Runtime exceptions are unchecked exceptions

Posted by Shashanka Mogaliraju    2014-11-07 12:23:40


ans is E

Posted by Shubham Bansal    2014-11-07 14:03:06


Ans is E...here we have three UserDefined classes AEx,BEx,CEx.. in this AEx and CEx are Checked Exceptions Bcz they are extending th RunTimeException class which is Checked Exception...

in the m1() we are saying throws AEx,BEx,CEx Means Any of these exceptions raised than this method is not handled,the caller of this method should handle this...so here noProblem...

m2() specifies throws as Exception so No problem here...

m3() here Exception wil thrown type of Aex so the program terminate abnormally But the No Compile time error Bcz it is an Unchecked Exception...

m4() here CEx exception wil thrown but in the throws we specify its parent class AEx so no Problem here....

m5() in finaally block Exception wil occur But that is Unchecked Exception  so no error

So all methods will compile

Posted by Uday Kumar    2014-11-07 15:34:08


ans is E

Posted by Atul Gupta    2014-11-08 16:38:49


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Vicky Maduri. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-10 01:17:18


© meritcampus 2019

All Rights Reserved.

Open In App