java 什么时候会抛出 CloneNotSupportedException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1143746/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:20:52  来源:igfitidea点击:

when would CloneNotSupportedException be thrown?

java

提问by user26270

I'm going through some old code and found the following:

我正在浏览一些旧代码,发现以下内容:

public class MyClass implements Cloneable {

    public Object clone() {
        Object o = null;
        try {
            o = super.clone();
        } catch (CloneNotSupportedException ex) {
        }
        return o;
    }

}

I've read the javadocs on Object.clone(), and I'm trying to figure out why this catch is even there. I mean, I understand it has to be there because Object.clone() throws it, but when would it ever get there, if I'm only extending Object by default, and this class is implmenting Cloneable? If this class was extended and the sub-class didn't implement Cloneable, is that what it's there for?

我已经阅读了 Object.clone() 上的 javadocs,我试图弄清楚为什么这个问题甚至存在。我的意思是,我知道它必须在那里,因为 Object.clone() 抛出它,但它什么时候到达那里,如果我只是默认扩展 Object,并且这个类正在实现 Cloneable?如果这个类被扩展而子类没有实现 Cloneable,那它是用来做什么的?

So is it OK to leave that catch block empty?

那么可以将该 catch 块留空吗?

回答by Jon Skeet

No, don't leave it empty. Log and throw a RuntimeException. Always do this for things that you think are impossible - that way, if the impossible eventually happens, it's treated as an unexpected error (which it is) rather than just returning nullas if nothing bad had happened.

不,不要让它空着。记录并抛出一个RuntimeException. 始终对您认为不可能的事情执行此操作 - 这样,如果不可能的事情最终发生,则将其视为意外错误(确实如此),而不是null像没有发生任何不好的事情一样返回。

Admittedly I really don'texpect you to ever see it, but the above is a generally good way to handle errors you shouldn't see...

诚然,我真的希望你看到它,但以上是处理你不应该看到的错误的一般好方法......

回答by Rodrigo Asensio

there is no problem if you re-throw or not the exception. Your object MyClass will never throw CloneNotSupportedEx because it is being thrown only when the interface is not implemented. See the javadoc for Clonable

如果您重新抛出异常,则没有问题。您的对象 MyClass 永远不会抛出 CloneNotSupportedEx,因为它仅在未实现接口时才会抛出。请参阅可克隆的 javadoc

By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. See {@link java.lang.Object#clone()} for details on overriding this method.

Note that this interface does notcontain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

按照惯例,实现此接口的类应该使用公共方法覆盖 Object.clone(受保护)。有关覆盖此方法的详细信息,请参阅 {@link java.lang.Object#clone()}。

请注意,这个接口并没有包含clone方法。因此,不能仅仅凭借对象实现了这个接口就克隆一个对象。即使以反射方式调用 clone 方法,也不能保证它会成功。