This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

CloneNotSupportedException does not require try block or throws clause

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CloneNotSupportedException is only thrown if the class does not support the Cloneable interface,. Since arrays implement Cloneable, it compiles fine.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.�
 
What's that smell? I think this tiny ad may have stepped in something.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic