wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes CloneNotSupportedException does not require try block or throws clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "CloneNotSupportedException does not require try block or throws clause" Watch "CloneNotSupportedException does not require try block or throws clause" New topic
Author

CloneNotSupportedException does not require try block or throws clause

John Zoetebier
Ranch Hand

Joined: Mar 28, 2003
Posts: 76
The rules for exception handling are: " Handle or declare a checked exception"
The program below uses method clone() of an array.
According to the API this method throws eNotSupportedException.
I would expect the compiler to reuire a try block or throws clause, but it compiles just fine.
Can anybody explain why this is ?
Praveen Kumar Mathaley
Ranch Hand

Joined: Apr 14, 2003
Posts: 45
All Arrays are considered to implement Cloneable Interface and also clone for arrays does a shallow copy of the objects in the array.
clone method of object is defined to throw the CloneNotSupportedException(Checked Exception) and it doesn't imply that all the derived classes should throw the exception.
Conclusion is that the clone method of the arrays doesn't throw CloneNotSupportedException
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
CloneNotSupportedException is only thrown if the class does not support the Cloneable interface,. Since arrays implement Cloneable, it compiles fine.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
John, I was going to suggest that you look at JLS 10.7. But then I noticed your example looks almost the same.
JLS 10.7
Notice in 10.7 that the clone() method for an array object overrides the clone() method of Object and catches the exception.
[ April 14, 2003: Message edited by: Marlene Miller ]
John Zoetebier
Ranch Hand

Joined: Mar 28, 2003
Posts: 76
The example comes from a mock exam of JQPlus 4.
It may well be a modification from the JLS example in
JLS 10.7
From other posting I understand that array overrides method Object.clone().
An overriding method can choose not to throw an exception. Hence the code does not have to handle the exception.
It seems that even compilers in the past did not know this and generated an error.
Is there any way, other than digging through the JLS, to know if there is an exception thrown or not?
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Is there any way, other than digging through the JLS, to know if there is an exception thrown or not?
The API doc for the clone() method of the Object class says this: �Note that all arrays are considered to implement the interface Cloneable.�
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CloneNotSupportedException does not require try block or throws clause
 
Similar Threads
clone method
Initialization of arrays
Threads and Clonning
clone method
clone() with arrays??