Files
openide/java/java-impl/resources/inspectionDescriptions/CloneDeclaresCloneNotSupported.html
2025-02-05 04:43:28 +00:00

27 lines
1.3 KiB
HTML

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