Menu
Topics Index
...
`

Try With Multiple Catch Blocks486


If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks486
Given the following hierarchy of exceptions what will be the output of the program?
class AEx extends Exception
{
}

class BEx extends Exception
{
}

class CEx extends Exception
{
}

class TestExceptions
{
    public static void main(String s[]) throws AEx, BEx, CEx
    {
        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 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.

d

Posted by Shubham Bansal    2015-02-09 15:40:18


ans a..  Java supports two keywords throw and throws . throw helps in throwing a new exception or re- throwing a caught exception. throws needs to the placed after the method signature to indicate the caller of the method what exceptions a method can throw.here  we have 1 main class and 3 classes which extends excption class main method 1st sop prints "Before A",thn methodA called with false as parameter ,if condition false here no exception get thrown ,similarly for other 2 methods

Posted by Maheshwari Natarajan    2015-02-09 19:31:51


Ans is C..here we have the Userdefined Exceptions...we can define user define exceptions by extending the Exception class..here we have the AEx,BEx,CEx user defined exceptions..all these are checked exceptions means programmer must handle this exceptions if not compiler will raise the error...in methodA() methodB() methodC() we are manually throwing the AEx,BEx,CEx by using the throw keyword and we are specifing throws at method defination so if the exception occurs than it must be handled by the caller...so here caller is main() so handling must be done by the main() in the main() we write throws AEx,BEx,CEx so if the exception occurs JVM will handle this Bcz main() is invoked by the JVM.

Execution:
in methodA() we are throwing the AEx Exception and we wrote throws clause with the AEx so no problem here
in methodB() we are throwing the BEx exception but we wrote the throws as AEx here compiler will raise an error here AEx is not Parent of the BEx..we must write BEx
so  no problem with mehtodC()

Posted by Uday Kumar    2015-02-10 03:25:38


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 Uday Kumar. 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-02-10 04:16:18


© meritcampus 2019

All Rights Reserved.

Open In App