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 ?
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
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 ]
Joined: Mar 28, 2003
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?
Joined: Mar 05, 2003
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.�
subject: CloneNotSupportedException does not require try block or throws clause