Topics Index

Try With Multiple Catch Blocks

If you need explanation Read this topic

If you need Answer Take test on this topic

Try With Multiple Catch Blocks
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");
        System.out.println("Before B");
        System.out.println("Before C");

    public static void methodA(boolean value)
        if(value) throw new AEx();

    public static void methodB(boolean value)
        if(value) throw new BEx();

    public static void methodC(boolean value)
        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.

c.    unreported exception AEx; must be caught or declared to be thrown if(value ) throw new AEx ();

Posted by Shubham Bansal    2015-01-07 12:28:30

ans is c  
unreported exception AEx; must be caught or declared to be thrown                                          if(value) throw new AEx();
unreported exception BEx; must be caught or declared to be thrown                                         if(value) throw new BEx();
unreported exception CEx; must be caught or declared to be thrown                                     if(value) throw new CEx();

Posted by Ramya Reddy Addulla    2015-01-07 12:51:46

ANS IS C (Compilation Error)

Here the concept of "EXCEPTION HANDLING" is used.

--Exception is  run time error in the program which comes due to logical errors in the programs.
In above program three classes are defined where 1st class extends exception class,2nd class extends 1st class n 3rd extends 2nd.

--In main method 3 methods are calledby passing "false" value to methodA(),methodB() and methodC() which accepts the boolean value.
In these methods three exceptions are thrown:
But these exceptions are only thrown there is no code to "CATCH" the exception.There "Compilation Error" comes while executing the program as "NO TRY CATCH BLOCK IS WRITTEN TO HANDLE THE THROWN EXCEPTION."

Posted by Mânïshå Mùlchåndânï    2015-01-07 16:18:27

Ans is we have the Userdefined Exceptions...we can define user define exceptions by extending the Exception 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 methodA() methodB() methodC() we are manually throwing the AEx,BEx,CEx by using the throw keyword but we are not handling these exceptions so here compiler will raise an error..if we put these statements in try catch than we can execute it

Posted by Uday Kumar    2015-01-07 16:19:42

This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Fuzail Ahmed. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-01-09 02:45:44

© meritcampus 2019

All Rights Reserved.

Open In App