在 Java 中是否有类似 .NET 的 NotImplementedException 之类的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2329358/
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
Is there anything like .NET's NotImplementedException in Java?
提问by devoured elysium
Is there anything like .NET's NotImplementedException
in Java?
NotImplementedException
在 Java 中有没有类似 .NET 的东西?
采纳答案by Ravi Wallau
Commons Langhas it. Or you could throw an UnsupportedOperationException
.
Commons Lang拥有它。或者你可以抛出一个UnsupportedOperationException
.
回答by Chris Dail
I think the java.lang.UnsupportedOperationException
is what you are looking for.
我认为这java.lang.UnsupportedOperationException
就是你要找的。
回答by Ready4Android
You could do it yourself (thats what I did) - in order to not be bothered with exception handling, you simply extend the RuntimeException, your class could look something like this:
你可以自己做(这就是我所做的)——为了不被异常处理所困扰,你只需扩展 RuntimeException,你的类可能看起来像这样:
public class NotImplementedException extends RuntimeException {
private static final long serialVersionUID = 1L;
public NotImplementedException(){}
}
You could extend it to take a message - but if you use the method as I do (that is, as a reminder, that there is still something to be implemented), then usually there is no need for additional messages.
您可以扩展它以接收消息 - 但是如果您像我一样使用该方法(即,作为提醒,仍有一些东西要实现),那么通常不需要额外的消息。
I dare say, that I only use this method, while I am in the process of developing a system, makes it easier for me to not lose track of which methods are still not implemented properly :)
我敢说,我只使用这种方法,在我开发系统的过程中,让我更容易不忘记哪些方法还没有正确实现:)
回答by Dormouse
No there isn't and it's probably not there, because there are very few valid uses for it. I would think twice before using it. Also, it is indeed easy to create yourself.
不,没有,也可能没有,因为它的有效用途很少。在使用它之前我会三思而后行。此外,创建自己确实很容易。
Please refer to this discussionabout why it's even in .NET.
请参阅有关为什么它甚至在 .NET 中的讨论。
I guess UnsupportedOperationException
comes close, although it doesn't say the operation is just not implemented, but unsupported even. That could imply no valid implementation is possible. Why would the operation be unsupported? Should it even be there?
Interface segregation or Liskov substitution issues maybe?
我想UnsupportedOperationException
接近了,虽然它并没有说该操作没有实现,但甚至不受支持。这可能意味着不可能进行有效的实施。为什么不支持该操作?它甚至应该在那里吗?可能是接口隔离或 Liskov 替换问题?
If it's work in progress I'd go for ToBeImplementedException
, but I've never caught myself defining a concrete method and then leave it for so long it makes it into production and there would be a need for such an exception.
如果它正在进行中,我会去ToBeImplementedException
,但我从来没有发现自己定义了一个具体的方法,然后把它搁置了很长时间,它使它进入生产,并且需要这样一个例外。
回答by Jens Bannmann
As mentioned, the JDK does not have a close match. However, my team occasionally has a use for such an exception as well. We could have gone with UnsupportedOperationException
as suggested by other answers, but we prefer a custom exception class in our base library that has deprecated constructors:
如前所述,JDK 没有密切匹配。但是,我的团队偶尔也会使用此类异常。我们本可以UnsupportedOperationException
按照其他答案的建议使用,但我们更喜欢基础库中已弃用构造函数的自定义异常类:
public class NotYetImplementedException extends RuntimeException
{
/**
* @deprecated Deprecated to remind you to implement the corresponding code
* before releasing the software.
*/
@Deprecated
public NotYetImplementedException()
{
}
/**
* @deprecated Deprecated to remind you to implement the corresponding code
* before releasing the software.
*/
@Deprecated
public NotYetImplementedException(String message)
{
super(message);
}
}
This approach has the following benefits:
这种方法有以下好处:
- When readers see
NotYetImplementedException
, they know that an implementation was planned and was either forgotten or is still in progress, whereasUnsupportedOperationException
says (in line with collection contracts) that something will never be implemented. That's why we have the word "yet" in the class name. Also, an IDE can easily list the call sites. - With the deprecation warning at each call site, your IDE and static code analysis tool can remind you where you still have to implement something. (This use of deprecation may feel wrong to some, but in fact deprecation is not limited to announcing removal.)
- The constructors are deprecated, not the class. This way, you only get a deprecation warning inside the method that needs implementing, not at the
import
line (JDK 9 fixed this, though).
- 当读者看到 时
NotYetImplementedException
,他们知道一个实现是计划好的,要么被遗忘,要么仍在进行中,而UnsupportedOperationException
(根据收集合同)说某些东西永远不会被实现。这就是为什么我们在类名中有“yet”这个词。此外,IDE 可以轻松列出调用站点。 - 通过在每个调用站点上的弃用警告,您的 IDE 和静态代码分析工具可以提醒您仍然需要实现的地方。(这种使用 deprecation 可能会让一些人觉得不对,但实际上deprecation 不仅限于宣布删除。)
- 不推荐使用构造函数,而不是类。这样,您只会在需要实现的方法中收到弃用警告,而不是
import
在行中(不过,JDK 9修复了此问题)。