- java.lang.UnsupportedOperationException
- "Thrown to indicate that the requested operation is not supported."
The
UnsupportedOperationException is used in some Java libraries and classes for methods of interfaces they implement that cannot or should not be implemented in this class' implementation.
This may be an
AntiPattern, as it violates
LiskovSubstitutionPrinciple.
But from a pragmatic perspective, it can be quite useful, such as for providing "read only" implementations of the collection classes, violating the
LiskovSubstitutionPrinciple because the interfaces were originally defined with an assumption that all collections would be updatable.
Lately, I've taken to configuring my IDE to implement all new methods it implements with "throws
UnsupportedOperationException();" rather than "return null;", "return 0;", nothing, or other similar construct.
With
TestDrivenDevelopment, it's more convenient to have the stack trace point me right at the method that needs to be implemented, rather than having to trace back from a later failure caused by a "return null;" or "return 0;".
I picked up this idea from Rod Johnson, at The
SpringExperienceConference (December 2005 in Miami, Florida).
--
JeffGrigg
CategoryException