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

Java

Java Problem Overview


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

Java Solutions


Solution 1 - Java

java.lang.UnsupportedOperationException

> Thrown to indicate that the requested operation is not supported.

Solution 2 - Java

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:

  1. Use the NotImplementedException from apache commons-lang which was available in commons-lang2 and has been re-added to commons-lang3 in version 3.2.

  2. Implement your own NotImplementedException.

  3. Throw an UnsupportedOperationException with a message like "Not implemented, yet".

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKrishna KumarView Question on Stackoverflow
Solution 1 - JavadfaView Answer on Stackoverflow
Solution 2 - JavasteffenView Answer on Stackoverflow
Solution 3 - JavaBenny NeugebauerView Answer on Stackoverflow
Solution 4 - JavaGuillaumeView Answer on Stackoverflow
Solution 5 - JavaAlireza FattahiView Answer on Stackoverflow