Menu
Topics Index
...
`

Try With Multiple Catch Blocks482


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks482
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 Exception
{
}

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

    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 CEx
    {
        if(value) throw new CEx();
    }
}

A. Before A
Before B
Before C
B. Before A
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.

Answer is A....whenever any method is likely to throw an exception ,either it should declare throws exception or we should catch that using try catch...Even the method that is calling the method which is likely to throw the exception should obey the above rule

Posted by Shashanka Mogaliraju    2014-11-24 13:07:10


Ans is A..here we have the Concept of the Exceptions..it is an runtime error which will cause the abnormal termination of the program...when the problem occurs system will gather the info about the problem and throws that object to the user if we didnot handle than program terminates..

certain situations may come we need to throw an Exception which is suitable for the problem..this can be achvied using the Userdefined Exceptions...we need to extend an Exception class to get the future of the Exceptions...

here we have AEx,BEx,CEx which all are extending Exception class and all these are checked Exceptions...means we must handle this if not compiler will raise an error...

thorws:it si an keyword which used to tell the system that exceptions raised in this method defination are not handled here the caller of the method should handle this exception...

thorw: it is an keyword which is used throw the Exceptions manually by the programmer...

in methods() we are thorwing the exceptions manually by the programmer so that handling should be done in main() so here we aer saying throws so that JVM will handle this Exception so no problem here so Corresponding Sop() will execute...

Posted by Uday Kumar    2014-11-24 13:08:06


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 N Rajasekhar Reddy. 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-25 02:52:00


© meritcampus 2019

All Rights Reserved.

Open In App