Menu
Topics Index
...
`

Exceptions and Methods


If you need explanation Read this topic

If you need Answer Take test on this topic

Exceptions and Methods
If expections AEx and BEx are defined as shown below, which of the methods in the ExceptionsTest class fail to compile?
class AEx extends Exception {}
class BEx extends AEx {}

class ExceptionsTest {

    void m1() {
        try { throwA(false); throwB(true); } catch (BEx aex) { }
    }
    void m2() {
        try { throwA(true); throwB(false); } catch (AEx bex) { }
    }
    void m3() throws AEx {
        try {
            throwA(false); throwB(true);
        } catch (BEx bex) {
            throw bex;
        }
    }
    void m4() throws BEx {
        try {
            throwA(true); throwB(false);
        } catch (AEx aex) {
            throw aex;
        }
    }

    void throwA(boolean throwEx) throws AEx {
        if (throwEx) throw new AEx();
    }
    void throwB(boolean throwEx) throws BEx {
        if (throwEx) throw new BEx();
    }
}

A. m1, m2, m3, m4
B. m1, m2, m3
C. m3, m4
D. m1, m4
E. m2, m3
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.

Ans is D..here we have the Userdefined Exceptions Concept..where we can define our own exception classes by extending the exception class..here we have the 2 Exception classes AEx and BEx class where AEx is Parent class and BEx is child class.

Execution:
in the m1() we are calling the throwA() by passing the false in throwA() we are throwing the AEx exception we specified AEx exception using te throws clause but in the m1() we put catch for handling the BEx excpetion here it wil raise an error

m2() we wrote AEx in the catch so here no problem Bcz AEx is Parent class so it can handle for BEx also

m3() we wrote throws class specifying AEx so it will handle both the AEx and BEx

m4() we call throwA() by passing the true so here we will throw exception so it will enter into the catch block in the m4() in the catch we are again throwing the Aex so in catch we didnot write any try catch and we wrote throws clause as BEx so here it will raise an error..

we will get compiler time errors Bcz these are checked exceptions

Posted by Uday Kumar    2015-02-10 09:46:26


d.

Posted by Shubham Bansal    2015-02-10 19:01:10


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. 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    2015-02-12 04:21:06


© meritcampus 2019

All Rights Reserved.

Open In App