Try With Multiple Catch Blocks484
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 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.
Answer is A.........Here the classes AEx ,BEx and CEx are all custom exceptions as they are extended from Exception class.....In the main method system.out.print statements are printed and no compile time error is thrown since we are asking the jvm to handle the exceptions both from the main method (by declaring main method throws AEx,BEx,CEx) and also from the methods called from the main method where actually throw new Exception is executed
Posted by Shashanka Mogaliraju 2014-10-29 10:21:50
A
Posted by Jekil Hansora 2014-10-29 11:02:28
a
Posted by Ananthoju Arun Chary 2014-10-29 15:43:32
Ans is A...here we have 3 user defined exceptions named with AEx,BEx,CEx..these all classes are extending Exception class all these are became Checked exceptions..
in main class we have three methods..methodoA() it throwing AEx exception
methodB() throwing BEx exception
methodC() thrwoing CEx Exception...
here we are saying there may b chnace of getting Exceptions..and all these are checked exceptions so that we must handle these Exceptions otherwise Compiler wil raise an error
throws--> is key word in java which is ued to indicate the system that Exceptions raied in this method defination wil not handled here..the caller of the method should handle this
methodA(),methodB(),methodC() all these are calling from the method main() so that hanlding must takes place at the main() only in main() we are handling all these Exceptions by specifying the throws
Posted by Uday Kumar 2014-10-30 04:05:39
A
Posted by Nichitha Veludandi 2014-10-30 08:30:53
This dose is now closed and the winners are Shashanka Mogaliraju, for 'First Correct Comment', Shubham Bansal, for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Vikas Tiyagi. 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:56:29
Copied and Edited Comment Merit Campus
Posted by Uday Kumar 2014-10-31 07:21:53