Hi All,
I am little bit confused because of exception throwing rule. Look at the following code -
try-catch block on line -1 throws InterruptedException and which is catched by e1 which is superclass of
InterruptedException.
throw statement on line 1 shows that it is throwing superclass and not the subclass.
But how come compiler come to know that the subclass is InterruptedException(while subclass is represented by superclass). And how come line 2 compiles fine, while apperantly there is no InterruptedException that is thrown.? Is not the InterruptedException thrown by try-catch -1 is supposed to be masked with "Exception e1" and the try-catch on line 0 sees it as only as Exception but not the InterruptedException?
Regards
Regards, Sharmila.
We will have a future if we save Nature!
Exceptions are thrown at runtime and at runtime we can catch the object types. So this type is more like the polymorphism concept i.e. subclass object and superclass reference type....just for the compiler.
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Thanks Nitish and Ravi for your help.
Sorry i could not get the reason for the behavior of the code. May be i did not ask the question explicitly.
Let me ask a question again in different way. Please see new topic.
Regards
Regards, Sharmila.
We will have a future if we save Nature!
Compiler is ok with line 2 because it sees that exception thrown in line 1 is Exception and assumes that at runtime the actual object could potentially be a subtype, so it allows for it. If in line 1 you throw say IOException, line 2 won't compile. Please correct me if I am wrong.
Do not threaten THIS beaver! Not even with this tiny ad:
a bit of art, as a gift, the permaculture playing cards