This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Polymorphism w.r.t. overloading

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


Output of the above code is "inside super" which is fine.

I used the same logic in the program


Source:http://www.examlab.tk/

And I thought the answer would be Finally-A Throwable Finally-B. However, answer is Finally-A Interrupted Finally-B. Could someone please explain.
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Casting does not change the class type of the object. So, even though the InterruptedException object was implicitedly casted to an Exception reference, and then implicitedly casted to a Throwable reference. The reference t is still referring to a InterrruptedException object, which is then rethrown.

Henry
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think catch statements don't act like method calls... like the program doesn't look at each catch statement and decide which one it fits into best. It looks at the first catch statement and sees if the exception will fit and in this case it does. The thrown exception would be caught by (Throwable t) if it hadn't already been caught. This is just my badly worded understanding as I am trying to learn this stuff same as you, so anyone please correct me.
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i change the program to



then the code does not compile which is fine. I am still not clear about the way "catch" is working.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

From your code I understand the following:

1. You have thrown an InterruptedException which is caught - variable e

2. Its thrown again - still an InterruptedException object

3. Finally always runs so prints "Finally-A"

4. The most specific catch statement will catch the exception - prints "Interrupted"

5. Finally always runs so prints "Finally-B"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic