The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Article: 
 Exception-Handling Antipatterns
Subject:  Why we need NoSuchMethodException?
Date:  2006-04-10 23:09:29
From:  jwenting
Response to: Why we need NoSuchMethodException?


Or you may have a need to implement an interface that you can't split up but need only 90% of its functionality.
This can easily be the case in large systems where an interface with say 5 methods should ideally have been 2 interfaces (maybe the second extending the first) but changing it now would break too much code (potentially hundreds or thousands of classes, possibly classes not under your control).
I've encountered such situations myself, where an interface declared a method that's logically part of that interface (say an interface defining CRUD operations on DAOs of some sort) but there are read-only entities. In order to have a common system of display logic those read-only entities need implement the interface but the creation and update methods make no sense for them.
Rather than letting them fail silently (of course an option) I'd prefer to throw an UnsupportedOperationException (rather than NoSuchMethodException) in such cases. Makes it immediately clear to the author of the class trying to do that update that his action isn't supported, instead of potentially having him look for days or weeks for the reason why the updates he sent to the system never were stored.



 Feed java.net RSS Feeds