Menu
Topics Index
...
`

Try With Multiple Catch Blocks491


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks491
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(BEx bx)
        {
            System.out.println("Exception B Occured");
        }
        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 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 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 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
in the example also we write similary first we write the Child(BEx) and than after we write the Parent AEx..so here in the second methodB() call will raise the Exception it will throw the BEx exception so it will throw back to the main() in the main() we are handling using the tr-catch so in the 1st catch it self it will handle..once the exception occurs than system wont executes the remaining statements in the try

Posted by Uday Kumar    2015-02-16 03:53:06


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 Mânïshå Mùlchåndânï. 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-17 03:58:22


© meritcampus 2019

All Rights Reserved.

Open In App