Menu
Topics Index
...
`

Try With Multiple Catch Blocks490


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks490
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(true);
            System.out.println("Before C");
            methodC(false);
        }
        catch(AEx ax)
        {
            System.out.println("Exception A Occured");
        }
        catch(BEx bx)
        {
            System.out.println("Exception B 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 A Occured
C. Before A
Before B
Exception B 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 D..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 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() in the main() we write the try with the 2 catches first catch is for AEx second catch for BEx..here AEx is the Parent and BEx is the Child

Execution:
in the Exception an try can have any number of catches But there is an some order to follow..the order of the catches must be like
Child
Parent
Grand Parent
but in the example we write first Parent after Parent we wrote Child than here compiler will raise an error saying Un-reachable statement..in java Unreachable statements are not allowed so Compiler will raise an error..Unreachable Bcz even we got the BEx exception in the  try than first catch() will handle so catch(BEx c) is never execute so it is unreachable statment..in java Parent class reference can refer Child class object

Posted by Uday Kumar    2015-02-13 08:46:22


d

Posted by Shubham Bansal    2015-02-13 19:23:25


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 ?????????? ?????. 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-16 04:46:48


© meritcampus 2019

All Rights Reserved.

Open In App