Krishna Kumar
Krishna Kumar

Reputation: 8191

What is the standard exception to throw in Java for not supported/implemented operations?

In particular, is there a standard Exception subclass used in these circumstances?

Upvotes: 435

Views: 257899

Answers (5)

dfa
dfa

Reputation: 116304

From the Java documentation:

Thrown to indicate that the requested operation is not supported.

java.lang.UnsupportedOperationException

Example usage:

throw new UnsupportedOperationException("Feature incomplete. Contact assistance.");

Upvotes: 588

Guillaume
Guillaume

Reputation: 18865

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.

Upvotes: 16

Alireza Fattahi
Alireza Fattahi

Reputation: 45465

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");
   } 
}

Upvotes: 7

steffen
steffen

Reputation: 16938

Differentiate between the two cases you named:

Upvotes: 261

Benny Code
Benny Code

Reputation: 54782

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.

Upvotes: 43

Related Questions