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");
}
}
}
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.
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).
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).
Varchala said:
8 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.
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.
Raj said:
7 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.
Arnold said:
1 decade 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).
Just follow how the Exception are subclassed and have it in reversed order (subclass first).
Jps said:
1 decade 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.
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.
Pranjali said:
7 years ago
Can anyone explain what exactly is happening in the program? I am little confused please help me.
Tejas Gowda said:
9 years ago
Can we write,
Catch(Exc1) & then catch(Exc0) & catch(Exception e).
Catch(Exc1) & then catch(Exc0) & catch(Exception e).
Pavan said:
9 years ago
@Tejas.
Yes, you can then the statement in catch(EXC 1) will execute.
Yes, you can then the statement in catch(EXC 1) will execute.
Ezazpasha said:
5 years ago
Can I throw a class as an exception, is this correct? Anyone, tell me.
Parikrma said:
1 decade ago
Why it gives error when we are changing catch block's order ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers