Menu
Topics Index
...
`

Try With Multiple Catch Blocks489


If you need explanation Read this topic

If you need Answer Take test on this topic

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

class BEx extends Exception
{
}

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(true);
            System.out.println("Before C");
            methodC(false);
        }
        catch(AEx ax)
        {
            System.out.println("Exception A Occured");
        }
    }

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

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

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

A. Before A
Before B
Before C
B. Before A
Before B
Exception A Occured
C. Compilation Error
D. Runtime 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.

B; in the try block first statement Before A is printed. then control goes to method A value is false so it goes to main method. the second sop statement printed then control goes to method B . here value is true so it raises exception then the catch  block will be executed as Exception A occured.

Posted by Bahunadam Narendra    2014-10-10 12:15:39


Ans is C...Exception is an runtime error which wil cause an abnormal termination of the program..

we have Exception class Which is Child class of Throwble..
we have two type pf exceptions one is checked and Unchecked..
checked exceptions are checked at compile time only if they not handling than compiler wil raise an error, un checked exceptions wont checked by the compiler...

here we have 3 Exception classes named as AEx,BEx,CEx..all these classes are extending the Class Excpetion which checked exception..means all these 3 classes become Checked Exceptions..

Coming to the Program

we have three methods methodA(),methodB(),methodC() all these are taking boolean value as argument..these are static methods means we can call without creating the object..

we are calling methodA().by passing the arg as false,in methodA() if its true we are thrwoing the object of Class AEx..
throw--> it is an keyword which is used to throw an Exception object manually by the Programmer
throws--> it is keyword which is used to tel the caller of method should handle the exceptions iy any thing occurs in this method..here from main() we are calling all these method and these methods used throws keyword..means ew must handle it in main() while calling..in main() we put try catch mechanisam..in catch we wrote AEx means this try wil handle ony the exception type of class AEx..
methodA(false) here we not throwing any exception..
methodB(true) here we are throing BEx class Object which is Checked exception..but we use the keyword throws means caller must handle it..so the compiler wil check in main() we are not handling excpetion we wrote code to handle only AEx class exception not BEx..so Compiler wil gv erroe..Bcz it is checked Exception

in methodC(false) we are not thrwoing any Exception by we are using throws keyword by saying throws BEx...means caller should handle the BEx exception bt we are not handling so Error wil give at this line also

Posted by Uday Kumar    2014-10-10 12:18:36


c

Posted by Shubham Bansal    2014-10-10 12:40:56


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm 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    2014-10-13 04:58:58


© meritcampus 2019

All Rights Reserved.

Open In App