Menu
Topics Index
...
`

Try With Multiple Catch Blocks492


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks492
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[])
    {
        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.

My Guess is Option D .. the reason is main() method is not handling AEx() exception which is superclass for all 3 exceptions and which can handle all 3 exceptions  which we are throwing via methods ..  So.. Compilation Error Occurs .. before running/Execution
..

Posted by ?????????? ?????    2015-02-05 06:28:59


d

Posted by Shubham Bansal    2015-02-05 08:52:36


ANS IS A
Before A
Before B
Before C

--In above Example Three classes are defined:
1.AEx which extends Exception class
2.BEx which extends Exception AEx
3.CEx which extends Exception BEx

--Here,concept of "EXCEPTION" is used.The class Exception and its subclasses are a form of Throwable that
indicates conditions that a reasonable application might want to catch.The class Exception and any subclasses
that are not also subclasses of RuntimeException are checked exceptions.

--In method main(),First "BEFORE A" will get printed.MethodA() is called wth the value false.Inside the method if condition is there.If boolean variable is true then AEx Class's Exception is thrown.

--After that,"BEFORE B" will get printed.MethodB() is called with the value false.Inside the method if condition
is there.If boolean variable is true then BEx Class's Exception is thrown,else nothing is done.

--After that,"BEFORE C" will get printed.MethodC() is called wth the value true.Inside the method if condition
is there.If boolean variable is true then CEx Class's Exception is thrown.

--But this method throws AEx class exception if any ERROR
occurs in the program.

--Control goes in catch block,but in catch block subclass
exception CEx is used and superclass exception can not be
assigned to subclass exception.

--So,Compilation error is there in program.

Posted by Mânïshå Mùlchåndânï    2015-02-05 14:34:33


This dose is now closed and the winners are Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. 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-06 03:41:42


Merit Campus ... Can You Please Check the Ans Again ... I think We will get Compilation Error  why because Every method is throwing AEx which is Not handled ...

Posted by ?????????? ?????    2015-02-06 06:27:14


© meritcampus 2019

All Rights Reserved.

Open In App