Java Programming - Exceptions - Discussion

Discussion Forum : Exceptions - Finding the output (Q.No. 10)
10.
What will be the output of the program?
class Exc0 extends Exception { } 
class Exc1 extends Exc0 { } /* Line 2 */
public class Test 
{  
    public static void main(String args[]) 
    { 
        try 
        {  
            throw new Exc1(); /* Line 9 */
        } 
        catch (Exc0 e0) /* Line 11 */
        {
            System.out.println("Ex0 caught"); 
        } 
        catch (Exception e) 
        {
            System.out.println("exception caught");  
        } 
    } 
}
Ex0 caught
exception caught
Compilation fails because of an error at line 2.
Compilation fails because of an error at line 9.
Answer: Option
Explanation:

An exception Exc1 is thrown and is caught by the catch statement on line 11. The code is executed in this block. There is no finally block of code to execute.

Discussion:
11 comments Page 1 of 2.

Ezazpasha said:   4 years ago
Can I throw a class as an exception, is this correct? Anyone, tell me.

Raj said:   5 years ago
When exception are occur in try block it will throw the Exception. This Exception handle in catch block but sub class write first in catch block. If we put super class first in catch block then it will show compile time error.

Pranjali said:   6 years ago
Can anyone explain what exactly is happening in the program? I am little confused please help me.

Varchala said:   7 years ago
@Maha.

All you should understand is when an exception is thrown by the child class i.e. exc1 as there is no catch handler for exc1 is present the parent ex0 handler is executed and if even the parent handler is not present then the super class catch i.e. Exception catch handler will be executed.

Now, relate this to Siddhartha's explanation.

Maha said:   7 years ago
I can't understand, explain it clearly.

Pavan said:   7 years ago
@Tejas.

Yes, you can then the statement in catch(EXC 1) will execute.

Tejas Gowda said:   8 years ago
Can we write,

Catch(Exc1) & then catch(Exc0) & catch(Exception e).

Jps said:   9 years ago
Exception is grand-parent of Exc1.
ExcO is parent of Exe1.
An exception Exc1 is thrown.

And is caught by the catch ExcO (1st catch) so,
So 2nd catch not executed.

So output is Ex0 caught.

Arnold said:   10 years ago
Exception subclasses first before the general Exception class itself... else, compiler will complain.

Just follow how the Exception are subclassed and have it in reversed order (subclass first).

SIDDHARTHA ROY NANDI said:   1 decade ago
Hi Parikrma, The reason is the oreders are:

Exception <TopMost SuperClass>
|
--> Exc0 <Immediate SubClass>
|
--> Exc1<SubClass of Exc0>

So, catch blocks will be selected in exact opposite order.
Otherwise, it'll cause an Unreachable Catch Block exception.Because,Exception of type Exc1 can be handled by both catch(Exception e) and catch(Exc0 e). So, if you don't follow the order,there is no necessity of catch(Exc1 e).


Post your comments here:

Your comments will be displayed after verification.