File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes clone() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "clone() method" Watch "clone() method" New topic

clone() method

Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
In the following example, why isn't the constructor required to declare CloneNotSupportedException in its 'throws' clause:

This program compiles and runs without any problem.
CloneNotSupportedException is derived from Exception, which makes it a checked exception.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

When a class implements a method, it is allowed to narrow the exception specification -- it's perfectly acceptable for an implementation of clone() to declare that it throws no exceptions.
I don't recall the JLS stating explicitly that the implementations of clone() in the array classes had empty exception specifications -- but if you think about it, it makes obvious sense.

[Jess in Action][AskingGoodQuestions]
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Yes Ernest, it does make sense if the implementation of array classes do not throw this exception.
Well, thanks for your response.
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
I found supporting evidence for what you said. This is an excerpt from the JLS:

10.7 Array Members
The members of an array type are all of the following:
1. The public final field length, which contains the number of components of the array (length may be positive or zero)
2. The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions
3. All the members inherited from class Object; the only method of Object that is not inherited is its clone method

Don't get me started about those stupid light bulbs.
subject: clone() method
Similar Threads
StackOverFlowError - Can some one tell me the reason
protected behaviour of clone method is surprising !
String reference remains same during clone()
Understanding superclass and subclass objects