Discussion :: Exceptions - Finding the output (Q.No.10)
|Parikrma said: (Jun 16, 2012)|
|Why it gives error when we are changing catch block's order ?|
|SIDDHARTHA ROY NANDI said: (Jul 8, 2012)|
|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).
|Arnold said: (Aug 10, 2014)|
|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).
|Jps said: (Oct 15, 2014)|
|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.
|Tejas Gowda said: (Mar 13, 2016)|
|Can we write,
Catch(Exc1) & then catch(Exc0) & catch(Exception e).
|Pavan said: (Dec 15, 2016)|
Yes, you can then the statement in catch(EXC 1) will execute.
|Maha said: (May 26, 2017)|
|I can't understand, explain it clearly.|
|Varchala said: (Jun 23, 2017)|
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.
|Pranjali said: (Mar 6, 2018)|
|Can anyone explain what exactly is happening in the program? I am little confused please help me.|
|Raj said: (Dec 11, 2018)|
|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.|
|Ezazpasha said: (Apr 30, 2020)|
|Can I throw a class as an exception, is this correct? Anyone, tell me.|
Post your comments here:
Email : (optional)
» Your comments will be displayed only after manual approval.