• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Littlebit different Exception?

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

line 1 shows that it is throwing superclass and not the subclass.


superclass reference is holding subclass object, i.e, line 1 is throwing InterruptedException object with superclass reference.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic