Menu
Topics Index
...
`

Try With Multiple Catch Blocks495


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks495
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(AEx ax) {
            System.out.println("Exception A Occured");
        }
        try {
            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
Exception A Occured
B. Before A
Exception A Occured
Before B
Exception A Occured
C. Before A
Exception A Occured
Before B
Before C
D. Before A
Exception A Occured
Before B
Exception A Occured
Before C
Exception A Occured
E. Compilation Error or 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.

Answer is C,
In first try block Before A is printed and then if condition is true so Exception A Occured is printed, for remaining two try block if condition do not execute so Before B and Before C is printed.

Posted by Amar Kumar Biradar    2014-02-11 12:39:21


C. is the answer. as methodA is true ,if in MethodA dsnt satisfies and hence exption ariases for A .remaining no exceptins arises

Posted by Shabaaz Shaikz    2014-02-11 12:43:44


Congratulations Amar Kumar Biradar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-12 03:50:26


Thanks Merit Campus ;)
Got amount in my account :)

Posted by Amar Kumar Biradar    2014-02-12 10:53:16


© meritcampus 2019

All Rights Reserved.

Open In App