clone()
methods that do not declare CloneNotSupportedException
.
If CloneNotSupportedException
is not declared, the method's subclasses will not be able to prohibit cloning
in the standard way. This inspection does not report clone()
methods declared final
and clone()
methods on final
classes.
Configure the inspection:
Use the Only warn on 'protected' clone methods option to indicate that this inspection should also warn on public clone()
methods.
The Effective Java book (second and third edition) recommends omitting the CloneNotSupportedException
declaration on public
methods, because the methods that do not throw checked exceptions are easier to use.
Example:
public class Example implements Cloneable {
// method doesn't declare 'throws CloneNotSupportedException'
protected Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}