Menu
Topics Index
...
`

Try With Multiple Catch Blocks497


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks497
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[]) throws AEx
    {
        try
        {
            System.out.println("Before A");
            methodA(true);
        }
        catch(AEx ax)
        {
            System.out.println("Exception A Occured");
        }

        try
        {
            System.out.println("Before B");
            methodB(false);
        }
        catch(BEx bx)
        {
            System.out.println("Exception B Occured");
        }

        try
        {
            System.out.println("Before C");
            methodC(true);
            System.out.println("After C");
        }
        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
Exception A Occured
Before B
Exception B Occured
Before C
After C
Exception C Occured
B. Before A
Exception A Occured
Before B
Exception B Occured
Before C
Exception C Occured
C. Before A
Exception A Occured
Before B
Before C
After C
Exception C Occured
D. Before A
Exception A Occured
Before B
Before C
Exception C Occured
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 user defines  Exceptions concept..in some situations we may need to throw
an specific exceptions by the user...in that casae we will devalop the an Exception class by extending
the Exception class and we we will throw the object of this class...here we have AEx n BEx n CEx user
defined exceptions..AEx is the Parent class for BEx and CEx

throws:- it is an keyword which is used to tell the system exceptions raised in this method not handle
here..these must handle by the caller of the method..if we write Parent Exception than it will applicable to its Childs also...

throw: it is used to throw the Exception Objects manually by the programmer..
Ex: throws Excption1,exceptio2...etc

Execution:

here we are calling the methodA() by passing the true...and condition is true so here we throwing the
AEx exception manually by using the throw and we write throws so AEx excption must handle in the main
the calling statement we write in the try-catch so no problem..so it will print Before A n Exception A
occured

next we are calling the methodB() by passing the false so here condition fails and no Exception is
thrown so catch in main wont get executed..prints "Before B:

calling method methodC() by passing the true so here exception is thrown and handles in the main Bcz
we write throws...prints "Before C" "After C" and "Exception C Occured"

Posted by Uday Kumar    2014-12-23 08:51:21


ANS IS D
--Three User defined Exceptions are created AEx,BEx,CEx
1) IN main control will first go in first try block and prints
BEFORE A
methoda(true) is called which throws object of exception AEx which makes the control to go into catch block with exception AEx and Exception A OCCURED will get printed.
2) In second try block BEFORE B will get printed.
as methodb() is called with false value then exception is not thrown and catch block is not executed.

3) In third try block BEFORE C will get printed.
methodc() is called with true value which throws the exception of class CEx due to this catch block containing object of CEx will be executed i.e.
EXCEPTION C OCCURED

And when exception occurs then remaining statements after the statement which generated exception are skipped i.e.AFTER A will not get executed.

Posted by Mânïshå Mùlchåndânï    2014-12-23 11:29:55


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Mânïshå Mùlchåndânï,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-12-24 03:52:37


© meritcampus 2019

All Rights Reserved.

Open In App