Friday, March 13, 2015

CloneNotSupportedException

java.lang.CloneNotSupportedException

CloneNotSupportedException is described in the javadoc comments as:

Thrown to indicate that the clone method in class Object has been called to clone an object, but that the object's class does not implement the Cloneable interface.

Applications that override the clone method can also throw this exception to indicate that an object could not or should not be cloned.
author: unascribed version: 1.11, 12/19/03 see: java.lang.Cloneable see: java.lang.Object#clone() since: JDK1.0

Where is this exception thrown?

Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown.

How is this exception thrown?

The following sub-sections identify where this exception is thrown, and how (or why) the code is throwing the exception.

Any source code quoted in this section is subject to the Java Research License unless stated otherwise.

java.lang.Enum.clone()

/**
     * Throws CloneNotSupportedException.  This guarantees that enums
     * are never cloned, which is necessary to preserve their 'singleton'
     * status.
     * @return (never returns)
     */
protected final Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.security.MessageDigest.clone()

/**    
     * Returns a clone if the implementation is cloneable.    
     * @return a clone if the implementation is cloneable.
     * @exception CloneNotSupportedException if this is called on an
     * implementation that does not support <code>Cloneable</code>.
     */
public Object clone() throws CloneNotSupportedException {
    if (this instanceof Cloneable) {
        return super.clone();
    } else {
        throw new CloneNotSupportedException();
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.security.MessageDigestSpi.clone()

/**    
     * Returns a clone if the implementation is cloneable.    
     * @return a clone if the implementation is cloneable.
     * @exception CloneNotSupportedException if this is called on an
     * implementation that does not support <code>Cloneable</code>.
     */
public Object clone() throws CloneNotSupportedException {
    if (this instanceof Cloneable) {
        return super.clone();
    } else {
        throw new CloneNotSupportedException();
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.security.Signature.clone()

/**
     * Returns a clone if the implementation is cloneable.
     * @return a clone if the implementation is cloneable.
     * @exception CloneNotSupportedException if this is called
     * on an implementation that does not support <code>Cloneable</code>.
     */
public Object clone() throws CloneNotSupportedException {
    if (this instanceof Cloneable) {
        return super.clone();
    } else {
        throw new CloneNotSupportedException();
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.security.SignatureSpi.clone()

/**
     * Returns a clone if the implementation is cloneable.
     * @return a clone if the implementation is cloneable.
     * @exception CloneNotSupportedException if this is called
     * on an implementation that does not support <code>Cloneable</code>.
     */
public Object clone() throws CloneNotSupportedException {
    if (this instanceof Cloneable) {
        return super.clone();
    } else {
        throw new CloneNotSupportedException();
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

No comments:

Post a Comment