• Post Reply Bookmark Topic Watch Topic
  • New Topic

Enforcing a concrete class method to throw an Exception  RSS feed

 
Hemant B. Kumar
Greenhorn
Posts: 11
Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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??
 
Sourabh Bawage
Ranch Hand
Posts: 33
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
overridden method can throw child class exception(or same) of the parent class method or no exception at all.....
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!