Javatpoint Logo
Javatpoint Logo

CloneNotSupportedException in Java with Examples

In Java, the CloneNotSupportedException is an exception that indicates that an attempt to clone an object has failed because the object does not implement the Cloneable interface. The Cloneable interface is a marker interface that indicates that an object can be cloned. When an object not implementing Cloneable is cloned, Java throws the CloneNotSupportedException.

Hierarchy:

Here is the hierarchy of the CloneNotSupportedException class:

As you can see from the hierarchy, the CloneNotSupportedException extends the Exception class, which in turn extends the Throwable class. A Java program can catch and handle the CloneNotSupportedException, an exception type.

A try-catch block can be used to handle this Exception, or it can be specified to be thrown in the method's signature using the throws keyword.

Syntax:

The syntax of CloneNotSupportedException in Java is:

Example of CloneNotSupportedException :

Filename: Dog.java

Output:

Dog [Breed=Labrador Retriever, Age=3]
Cloning not supported: Cloning not supported for class Dog.

java.lang.CloneNotSupportedException: Cloning not supported for class Dog.
	at Dog.clone(CloneNotSupportedExceptionDemo.java:22)
	at CloneNotSupportedExceptionDemo.main(CloneNotSupportedExceptionDemo.java:36)






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA