CloneNotSupportedException should extends RuntimeException not Exception
Eamonn McManus
eamonn at mcmanus.net
Sun Oct 14 21:41:12 UTC 2012
Brian Goetz <brian.goetz at oracle.com> writes:
> try { clone() }
> catch (RuntimeException e) { ... }
> catch (CloneNotSupportedException e1) { .... }
I guess that means the change is not binary compatible either, since before
the change the second catch block would run but after it the first one
would.
Éamonn
2012/10/14 Brian Goetz <brian.goetz at oracle.com>
> I believe this change is not source compatible. If a user says:
>
> try { clone() }
> catch (RuntimeException e) { ... }
> catch (CloneNotSupportedException e1) { .... }
>
> this compiles today but would fail to compile under this change.
>
> On Oct 14, 2012, at 7:06 PM, Mike Duigou wrote:
>
> > Seems reasonable to me at first glance. I am still reviewing this but
> wanted to add two clarify notes:
> >
> > - This change means that CloneNotSupportedException is no longer a
> checked exception. This change is generally harmless.
> >
> > - Cases where CloneNotSupportedException is being caught were probably
> added because it was a checked exception. They can be safely left in.
> >
> > Mike
> >
> >
> > On Oct 14 2012, at 09:19 , Remi Forax wrote:
> >
> >> Hi everybody,
> >> CloneNotSupportedException is thrown when a developer calls
> Object.clone() and forget to mark the current object as Cloneable. Because
> it's clearly a developer error, CloneNotSupportedException should be a
> subtype of RuntimeException and not of Exception.
> >>
> >> I believe this change is backward compatible because RuntimeException
> is a subclass of Exception,
> >> so I propose to first change CloneNotSupportedException to extends
> RuntimeException.
> >>
> >> diff -r ff641c5b329b
> src/share/classes/java/lang/CloneNotSupportedException.java
> >> --- a/src/share/classes/java/lang/CloneNotSupportedException.java Sat
> Oct 13 10:15:57 2012 +0100
> >> +++ b/src/share/classes/java/lang/CloneNotSupportedException.java Sun
> Oct 14 18:16:35 2012 +0200
> >> @@ -42,7 +42,7 @@
> >> */
> >>
> >> public
> >> -class CloneNotSupportedException extends Exception {
> >> +class CloneNotSupportedException extends RuntimeException {
> >> private static final long serialVersionUID = 5195511250079656443L;
> >>
> >> /**
> >>
> >>
> >> And then to clean up the whole JDK (in several patches) to remove all
> the unnecessary
> >> try/catch like the one in by example ArrayList.clone()
> >>
> >> public Object clone() {
> >> try {
> >> ArrayList<?> v = (ArrayList<?>) super.clone();
> >> v.elementData = Arrays.copyOf(elementData, size);
> >> v.modCount = 0;
> >> return v;
> >> } catch (CloneNotSupportedException e) {
> >> // this shouldn't happen, since we are Cloneable
> >> throw new InternalError(e);
> >> }
> >> }
> >>
> >> will become
> >>
> >> public Object clone() {
> >> ArrayList<?> v = (ArrayList<?>) super.clone();
> >> v.elementData = Arrays.copyOf(elementData, size);
> >> v.modCount = 0;
> >> return v;
> >> }
> >>
> >>
> >> cheers,
> >> Rémi
> >>
> >
>
>
More information about the core-libs-dev
mailing list