对于不支持/实现的操作,在 Java 中抛出的标准异常是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/829136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the standard exception to throw in Java for not supported/implemented operations?
提问by Krishna Kumar
In particular, is there a standard Exception
subclass used in these circumstances?
特别是,是否有Exception
在这些情况下使用的标准子类?
采纳答案by dfa
java.lang.UnsupportedOperationException
java.lang.UnsupportedOperationException
Thrown to indicate that the requested operation is not supported.
抛出以指示不支持请求的操作。
回答by Guillaume
If you want more granularity and better decription, you could use NotImplementedException from commons-lang
如果你想要更多的粒度和更好的描述,你可以使用commons-lang 中的NotImplementedException
Warning: Available before versions 2.6 and after versions 3.2, only.
警告:仅在 2.6 版之前和 3.2 版之后可用。
回答by steffen
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
NotImplementedException
from apache commons-langwhich was available in commons-lang2 and has been re-added to commons-lang3 in version 3.2.Implement your own
NotImplementedException
.Throw an
UnsupportedOperationException
with a message like "Not implemented, yet".
要表明不支持并且很可能永远不会支持所请求的操作,请抛出
UnsupportedOperationException
.要指示请求的操作尚未实现,请在以下选项中进行选择:
使用在 commons-lang2 中可用并在 3.2 版中重新添加到 commons-lang3 中的
NotImplementedException
from apache commons-lang。实施您自己的
NotImplementedException
.抛出
UnsupportedOperationException
类似“尚未实现”的消息。
回答by Benny Neugebauer
If you create a new (not yet implemented) function in NetBeans, then it generates a method body with the following statement:
如果您在NetBeans 中创建一个新的(尚未实现的)函数,那么它会生成一个包含以下语句的方法体:
throw new java.lang.UnsupportedOperationException("Not supported yet.");
Therefore, I recommend to use the UnsupportedOperationException.
因此,我建议使用UnsupportedOperationException。