Inspectopedia Help

'clone()' does not declare 'CloneNotSupportedException'

Reports clone() methods that do not declare throws CloneNotSupportedException.

If throws 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 only warn on protected 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; } } }

Inspection options

Option

Type

Default

Only warn on 'protected' clone methods

Checkbox

true

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3

Plugin:

Java, 233.SNAPSHOT

Last modified: 13 July 2023