• 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

exception while overriding

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,please resolve the problem of exception for overriding.Actually ,if super class declares a checked exception,then the overriding method cannot declare a new exception,or a cheked exception of broader type.

But the following gives error;
class A
{
void method() throws Exception
{

}

}

class B extends A
{
void method()
{

}

}
In the above scenario,the overriding method cannot declare a new exception or a checked exception of broader type ,But it is showing the error.Please solve this problem.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing wrong with this code; it would compile fine. Have you remembered to PostRealCode?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your description rings true but there was something missing from your example. Let's say you wrote class A:

And I started using it:

You told me to handle IOException and I did. What if the compiler let somebody write a new class B:

And what if somebody passed an instance of B into my method? I'd be totally unprepared to handle a SQLException because A never told me it might happen. That would be bad enough that the compiler just doesn't let you write B that way.

There's a neat guideline called the Liskov Substition Principle that says if B extends A then any code that works with an instance of A should work equally well with an instance of B. Throwing unexpected exceptions would certainly break that rule!
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic