In particular, is there a standard Exception subclass used in these circumstances?
5 Answers
From the Java documentation:
Thrown to indicate that the requested operation is not supported.
Example usage:
throw new UnsupportedOperationException("Feature incomplete. Contact assistance."); 4 Comments
RuntimeException. You will get no feedback or assistance in handling these exceptions during compile time. If this is used for a stubbed method or a work-in-progress you should use some kind of checked exception.null value. I prefer having a method returning UnsupportedOperationException. I use this myself and rewrite any existing code with this exception, when obviously no logic was implemented.Differentiate between the two cases you named:
To indicate that the requested operation is not supported and most likely never will, throw an
UnsupportedOperationException.To indicate the requested operation has not been implemented yet, choose between this:
Use the
NotImplementedExceptionfrom apache commons-lang which was available in commons-lang2 and has been re-added to commons-lang3 in version 3.2.Implement your own
NotImplementedException.Throw an
UnsupportedOperationExceptionwith a message like "Not implemented, yet".
3 Comments
If you create a new (not yet implemented) function in NetBeans, then it generates a method body with the following statement:
throw new java.lang.UnsupportedOperationException("Not supported yet."); Therefore, I recommend to use the UnsupportedOperationException.
Comments
If you want more granularity and better description, you could use NotImplementedException from commons-lang
Warning: Available before versions 2.6 and after versions 3.2 only.
Comments
The below Calculator sample class shows the difference
public class Calculator() { int add(int a , int b){ return a+b; } int dived(int a , int b){ if ( b == 0 ) { throw new UnsupportedOperationException("I can not dived by zero, not now not for the rest of my life!") }else{ return a/b; } } int multiple(int a , int b){ //NotImplementedException from apache or some custom excpetion throw new NotImplementedException("Will be implement in release 3.5"); } }
nulland you accidentally used it (or someone else did) you would getNullPointerExceptionswhich are less obvious thanUnsupportedOperationExceptionsin this case. Just an example.