aspose file tools*
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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "clone() method" Watch "clone() method" New topic
Author

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.
Thanks
Harwinder
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
Regards
Harwinder
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Ernest
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

Thanks
Harwinder
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clone() method