Menu
Topics Index
...
`

Try With Multiple Catch Blocks487


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks487
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");
        }
    }

    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. 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.

Ans is B..here we have the user defined exceptions concept which means we are creating the required exception class by extending the features of an Exception class..here we have AEx,BEx,CEx are the user defined exception classes...and all these are the checked exception Bcz they are extending the Exception class which is Checked
Execution:

Coming to main() here we are printing the "Before A" and next we are methodA() by passing the false as arg..in methodA() we are throwing Exception if the arg is true but here arg is false so no exception wil thrown..here we wrote throws AEx at methodA() which means if Exception occurs in this method the caller of the method should handle this..

next we wre printing the "Before B" and next we are methodB() by passing the true as arg..in methodB() we are throwing Exception if the arg is true so we wre throwing the Exception BEx .here we wrote throws AEx at methodB() which means if Exception occurs in this method the caller of the method should handle..here we are calling from the main so main should handle...so we put the statements in the try - catch so Exception will handle and prints "Exception A Occured"
so once the Exception occured it will skip the remaining statements in the try block so methodC() wont invoked

Posted by Uday Kumar    2015-01-26 09:20:18


b

Posted by Shubham Bansal    2015-01-26 10:55:41


ANS IS A

Before A
Before B
Before C

--In above Example Three classes are defined:
1.AEx which extends Exception class
2.BEx which extends Exception AEx
3.CEx which extends Exception BEx

--Here,concept of "EXCEPTION" is used.The class Exception and its subclasses are a form of Throwable that
indicates conditions that a reasonable application might want to catch.The class Exception and any subclasses
that are not also subclasses of RuntimeException are checked exceptions.

--In method main(),First "BEFORE A" will get printed.MethodA() is called wth the value false.Inside the method
if condition is there.If boolean variable is true then AEx Class's Exception is thrown.

--After that,"BEFORE B" will get printed.MethodB() is called wth the value true.Inside the method if condition
is there.If boolean variable is true then BEx Class's Exception is thrown.As this method throws AEx class's exception then
it is responsibility of class AEx to handle Exception.
As this class extends Exception class and nothing is written to handle the exception so nothing will get executed.

--After that,"BEFORE C" will get printed.MethodC() is called wth the value false.Inside the method if condition
is there.If boolean variable is true then BEx Class's Exception is thrown.

Posted by Mânïshå Mùlchåndânï    2015-01-26 15:30:51


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-01-27 07:42:24


© meritcampus 2019

All Rights Reserved.

Open In App