特别是,在这些情况下是否使用了标准Exception子类?


java.lang.UnsupportedOperationException

抛出,表示不支持所请求的操作。


如果你想要更细的粒度和更好的描述,你可以使用common -lang中的NotImplementedException

警告:仅在版本2.6之前和版本3.2之后可用。


区分你提到的两个案例:

要指出所请求的操作不受支持,而且很可能永远不会支持,可以抛出UnsupportedOperationException。 要指示所请求的操作尚未实现,请在以下两者之间进行选择: 使用apache common -lang中的NotImplementedException异常,该异常在commons-lang2中可用,并在3.2版中重新添加到commons-lang3中。 实现你自己的NotImplementedException。 抛出一个UnsupportedOperationException,并带有类似“尚未实现”的消息。


如果你在NetBeans中创建了一个新的(尚未实现的)函数,那么它会生成一个带有下面语句的方法体:

throw new java.lang.UnsupportedOperationException("Not supported yet.");

因此,我建议使用UnsupportedOperationException。


下面的Calculator示例类显示了两者的区别

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