Menu
Topics Index
...
`

Try With Multiple Catch Blocks481


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks481
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 Exception
{
}

class TestExceptions
{
    public static void main(String s[])
    {
        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 BEx
    {
        if(value) throw new BEx();
    }

    public static void methodC(boolean value) throws CEx
    {
        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 C.
AEx,BEx and CEx extends from Exception, so they will act as unchecked exceptions.. Even though we are throwing the exceptions in particular method we have to handle them in main method also using try-catch or throws.. we are not handling those exceptions in main method.. so it gives compilation error.

Posted by Gopinath Manchikanti    2014-10-29 03:40:49


Ans is C since we are throwing exception of a particular classess where it is not able catch it and handle it nor declaration of thrown exception and abnormal termination of program is taken place at statement methodA(false).

Posted by Ramesh Velpukonda    2014-10-29 03:51:11


Ans is C..here we have 3 user defined exception classes named with AEx,BEx,CEx    
in the main class we have 3 methods named with methodA() and methodB() and methodC()
throws key which is tells that in this function defination there may b chance getting Exceptions so that the caller of the method should handle this...

here all three Exceptions are Checked Exceptions which compiler check if we are not handling it than it wil raise an compilation error...

here methodA() saying it may throw AEx(Checked Exception) but in calling (in main() ) we are not handling so compiler wil raise error here...
similar for remaining 2 functions also....

Posted by Uday Kumar    2014-10-29 06:55:54


c

Posted by Shubham Bansal    2014-10-29 07:20:00


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Šâí Râm. 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-10-31 05:48:44


© meritcampus 2019

All Rights Reserved.

Open In App