Menu
Topics Index
...
`

Try With Multiple Catch Blocks493


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks493
Given the following hierarchy of exceptions what will be the output of the program?
class AEx extends Exception
{
}

class BEx extends AEx
{
}

class CEx extends BEx
{
}

class TestExceptions
{
    public static void main(String s[]) throws AEx
    {
        try
        {
            System.out.println("Before A");
            methodA(false);
            System.out.println("Before B");
            methodB(false);
            System.out.println("Before C");
            methodC(true);
        }
        catch(CEx cx)
        {
            System.out.println("Exception C Occured");
        }
    }

    public static void methodA(boolean value) throws AEx
    {
        if(value) throw new AEx();
    }

    public static void methodB(boolean value) throws AEx
    {
        if(value) throw new BEx();
    }

    public static void methodC(boolean value) throws AEx
    {
        if(value) throw new CEx();
    }
}

A. Before A
Before B
Before C
B. Before A
Before B
Exception C Occured
C. Before A
Before B
Before C
Exception C Occured
D. Compilation Error
Topic: Java Throw Keyword - Java Throws Keyword

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 C..here we have the Userdefined Exceptions...we can define user
define exceptions by extending the Exception class..here we have the
AEx,BEx,CEx user defined exceptions..all these are checked exceptions
means programmer must handle this exceptions if not compiler will raise
the error...in methodA() methodB() methodC() we are manually throwing
the AEx,BEx,CEx exceptions by using the throw keyword and we are
specifing throws at method defination so if the exception occurs than it
must be handled by the caller...so here caller is main() so handling must
be done by the main()
Execution:
in the main we are calling the methodA() by passing the false so here we are not throwing the exception so here wont raise an exception,next we are calling methodB() by passing the false so here also wont throw exception bcz false.next we are passing true so here exception will throw  here we are throwing the CEx exception so exception re thrown to the main() in the main() we put the statements in the catch so in the catch it will print
Exception C Occured

Posted by Uday Kumar    2015-02-24 03:57:38


c

Posted by Shubham Bansal    2015-02-24 06:53:58


http://www.kayads.com/index.php?r=smart78620

Posted by Anmol Khan    2015-02-24 07:36:38


ANS IS C

Here the concept of Exception is used.

Here three classes are declared:
AEX extends Exception
BEx extends AEx
CEx extends BEx

Some methods are called and they are written in try block.catch block holds the reference of class CEx.

First of all Before A will get printed.after that methodA() is called with false value.in method if true is passed then exception is thrown so here no exception will be thrown.

---After that BEFORE B will get printed.mehodB() is called with false value so no exception is thrown.

--After that BEFORE C will get printed and methodC () is called with true value so class CEx's exception will be thrown to catch block.

--As catch block reference matches with thrown exception object then EXCEPTION OCCURED will gt printed.

OUTPUT:
Before A
Before B
Before C
Exception occured

Posted by Mânïshå Mùlchåndânï    2015-02-24 08:34:15


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. 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-25 01:43:55


© meritcampus 2019

All Rights Reserved.

Open In App