Skip to content

Instantly share code, notes, and snippets.

@javamultiplex
Created October 21, 2017 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javamultiplex/50a05c58bdcd71c7ef13544d761ab012 to your computer and use it in GitHub Desktop.
Save javamultiplex/50a05c58bdcd71c7ef13544d761ab012 to your computer and use it in GitHub Desktop.
Example of CloneNotSupportedException present in java.lang package
package com.javamultiplex.java.lang.exceptions;
/**
* @author Rohit Agarwal
* @version 1.0
* @category java.lang/Exception
* @since JDK 1.0
*/
class Employee {
private String name;
public Employee(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "Employee[Name= " + name + "]";
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class CloneNotSupportedExceptionDemo {
public static void main(String[] args) {
Employee emp1 = new Employee("Rohit");
System.out.println(emp1);
try {
/*
* CloneNotSupportedException will be thrown because Employee class
* not implemented Cloneable interface.
*/
Employee emp2 = (Employee) emp1.clone();
System.out.println(emp2);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment