Menu
Topics Index
...
`

Try With Multiple Catch Blocks485


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks485
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
    {
        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 AEx
    {
        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.

Ans is Option A... Execution starts from main() method after execution of main() method it will print Before A after that it will call method methodA(false) of Class TestException inside this method we have if condition we are passing "false" as parameter so if-condition will fail after that compiler will return to main() method and it will print "Before B" after that it will go to methodB(false) of Class inside this method we have if-condition it will fail bcz here also we are passing "false" as parameter so it won't execute throw- statement then compiler will return to main() methdo and again it will print "BeforeC" after that it will call methodC(false) inside this method we have if-condition again it will fail bcz we are passing "false" as parameter so   throw-statement won't execute..... that's why Output is                                                     BeforeA                                                                        BeforeB                                                                                                                          BeforeC

Posted by Bhagi Bhagyasri    2015-01-22 12:36:16


a

Posted by Shubham Bansal    2015-01-22 14:56:13


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 false.Inside the method if condition is there.If boolean variable is true then BEx Class's Exception is thrown.

--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-22 17:38:51


This dose is now closed and the winners are Bhagi Bhagyasri,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. 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-23 05:01:55


© meritcampus 2019

All Rights Reserved.

Open In App