Java中是否有类似。net的NotImplementedException ?


当前回答

Commons Lang手上有。或者你可以抛出一个UnsupportedOperationException。

其他回答

如前所述,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);
    }
}

这种方法有以下好处:

When readers see NotYetImplementedException, they know that an implementation was planned and was either forgotten or is still in progress, whereas UnsupportedOperationException 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).

本着Stackoverflow是Reddit和Wikipedia的结合的精神,这里有一些与问题相关的附加信息,也可以是问题的答案。

当你要求NetBeans IDE创建一个缺失的实现时,它会使用UnsupportedOperationException:

void setPropertiesWithReader(IDataReader rdr)
{
   throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}

如果它对NetBeans足够好,对我们也足够好。

我认为java.lang.UnsupportedOperationException就是你要找的。

你可以自己做(我就是这么做的)——为了不被异常处理所困扰,你只需要扩展RuntimeException,你的类看起来就像这样:

public class NotImplementedException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public NotImplementedException(){}
}

您可以扩展它以接收消息—但是如果您像我这样使用该方法(也就是说,作为提醒,仍然有一些东西需要实现),那么通常不需要额外的消息。

我敢说,我只使用这种方法,当我在开发一个系统的过程中,使我更容易不失去跟踪哪些方法仍然没有正确实现:)

Commons Lang手上有。或者你可以抛出一个UnsupportedOperationException。