Inspectopedia
 
2024.3

'clone()' does not declare 'CloneNotSupportedException'

Warning
New
Last modified: 03 December 2024

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: