Blog |

How to Handle the Clone Not Supported Exception in Java

How to Handle the Clone Not Supported Exception in Java
Table of Contents

The CloneNotSupportedException is an exception in Java that is thrown to indicate that the clone() method in class Object was called to clone an object, but that 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.

 

What Causes CloneNotSupportedException

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal to create a clone of that class. Invoking Object.clone() on an instance that does not implement the Cloneable interface results in the CloneNotSupportedException being thrown.

By convention, classes that implement the Cloneable marker interface should override the protected Object.clone() method with a public method.

 

CloneNotSupportedException Example

Here is an example of an CloneNotSupportedException thrown when an object is cloned, and the object’s class does not implement the Cloneable interface:

class Person {
    private String name;

    public Person(String name) {
        super();
        this.name = name;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class CloneNotSupportedExceptionExample {
    public static void main(String[] args) {
        Person p = new Person("John");

        try {
            p.clone();
        } catch (CloneNotSupportedException cnse) {
            cnse.printStackTrace();
        }
    }
}

Since the Person class does not implement the Cloneable interface, it is not legal to create a clone of that class using Object.clone(). Therefore, running the above code throws the CloneNotSupportedException exception:

java.lang.CloneNotSupportedException: Person
    at java.base/java.lang.Object.clone(Native Method)
    at Person.clone(CloneNotSupportedExceptionExample.java:11)
    at CloneNotSupportedExceptionExample.main(CloneNotSupportedExceptionExample.java:20)

 

How to Handle CloneNotSupportedException

To avoid the CloneNotSupportedException, the Cloneable interface should be implemented by the class whose objects need to be cloned. This indicates to the Object.clone() method that it is legal to create a clone of that class and helps avoid the CloneNotSupportedException.

Since Cloneable is a marker interface, there is no method to implement after implementing the interface.

The earlier example can be updated with the above changes accordingly:

class Person implements Cloneable {
    private String name;

    public Person(String name) {
        super();
        this.name = name;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class CloneNotSupportedExceptionExample {
    public static void main(String[] args) {
        Person p = new Person("John");

        try {
            p.clone();
        } catch (CloneNotSupportedException cnse) {
            cnse.printStackTrace();
        }

        System.out.println("Continuing execution...");
    }
}

Here, the Person class was updated to implement the Cloneable interface, which helps avoid the CloneNotSupportedException. Running the above code produces the correct output as expected:

Continuing execution...

 

Track, Analyze and Manage Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape