Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Enforcing a concrete class method to throw an Exception

 
Greenhorn
Posts: 11
Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

How can I enforce a concrete class method (implementing an interface method) to throw an exception?
I tried something like this, and expected to get a compile time error in the class's method definition, but it compiled successfully

I understand that the only rule which works while overriding a method is - The method defintion in the sub-class/concrete class should not be more restrictive.
But if I want to enforce it anyhow, is there a way??
 
Ranch Hand
Posts: 33
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
overridden method can throw child class exception(or same) of the parent class method or no exception at all.....
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A better question: why do you want to enforce it?

If the method can throw a checked exception, the compiler will make sure it's declared. If it can't, there is no reason to declare it.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hemant B. Kumar wrote:
I understand that the only rule which works while overriding a method is - The method defintion in the sub-class/concrete class should not be more restrictive.
But if I want to enforce it anyhow, is there a way??



You have no control over whether or not the method actually does throw an exception since the implementer is free to do whatever he likes as long as he meets the contract. Also, since the user of the new class should be creating references to the interface and not the new class the user will have to code as if the new class actually can throw the exception.
 
Hemant B. Kumar
Greenhorn
Posts: 11
Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if I just want to stop the class implementor from making that mistake while implementing the interface exposed by a framework?
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hemant B. Kumar wrote:What if I just want to stop the class implementor from making that mistake while implementing the interface exposed by a framework?


Why should it be classed as a mistake? You might be able to do something with reflection before calling the method but why would you want to go to all that trouble?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the method can't throw a checked exception, then it's not a mistake to omit the declaration. It won't cause any problems.

If the method can throw a checked exception, the compiler will enforce this for you.

If it's an unchecked exception, it doesn't matter anyway - the declaration would only be there for documentation.
 
Marshal
Posts: 76472
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The idea of the Exception is that you are signalling to the user

I cannot make this method cope with XYZ

But the overridden method is a refinement, and whoever writes that may have different conditions, and can cope with XYZ. So it is a design decision, not a mistake, to declare no Exceptions.
If you insist on that method always declaring such an Exception, you will have to mark it final.
 
Tell me how it all turns out. Here is a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic