• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clone() method

 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 150
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic