8
public class test implements Cloneable {
    @Override
    public test clone() {
        return (test) super.clone();
    }

    public static void main(String[] args) {
        new test().clone();
    }
}

I get error: unreported exception CloneNotSupportedException when I try to compile this (at line 4, not the main). As far as I can tell, the entire purpose of implementing Cloneable is to get rid of the exception.

  • Is there a way to use super.clone() without throwing or catching the exception?
  • Does the interface actually do anything?
H.v.M.
  • 1,348
  • 3
  • 16
  • 42

3 Answers3

8

Is there a way to use super.clone() without throwing or catching the exception?

No because Object#clone() (the method you are calling with super.clone()) declares it.

Does the interface actually do anything?

Yes, but very little: if you don't implement it, Object#clone() will actually throw the declared exception.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
5

You need to handle CloneNotSupportedException exception, Class name should be start with capital letter.

Is there a way to use super.clone() without throwing or catching the exception?

If you don't want to handle exception inside clone method then use throws keyword it will propagate your exception to called method.

 public class Test implements Cloneable {
    @Override
    public Test clone() throws CloneNotSupportedException{
         return (Test) super.clone();
    }

    public static void main(String[] args) {
        try {
            new Test().clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

Does the interface actually do anything?

public interface Cloneable

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. read more

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
  • You have not handled the exception arising in main method – User27854 Jan 29 '15 at 12:09
  • 1
    @user2900314 And which exception should that be? – Tom Jan 29 '15 at 12:10
  • (new test().clone() will throw an exception and the exception is CloneNotSupportedException. – User27854 Jan 29 '15 at 12:11
  • @user2900314 Funny. Well, then explain please, how the `main` method should throw this exception. But mind first, that the only method, who could throw this exception is `super.clone()` and this call is inside a `try/catch` block. – Tom Jan 29 '15 at 12:15
  • @user2900314 I have edited my code, previous code is also correct because it was handling `Compile Time` exception in clone method. and if any exception arised at run time that will take care by java – atish shimpi Jan 29 '15 at 12:15
  • @Tom, you should seriously needs to do study more about Exception. Once you are done lets talk. – User27854 Jan 29 '15 at 12:19
  • @user2900314 He catched the `CloneNotSupportedException` in his own `clone` method (that wasn't overriding the `Object#clone` method, due to the missing `throws` clause), so how should that exception reach the `main` method? But anyway, throwing the exception to the calling method is the better of "handling" exceptions. – Tom Jan 29 '15 at 12:22
2

implementing Cloneable(marker interface: no methods) doesn't mean to get rid off Exception. its simple, you are telling compiler that in future you can create clone of the objects of implementing class. if you don't override the clone method automatically it will call Object class method.

  1. you have to throws Exception as its signature of overridden method.
  2. its to identify that your object can be cloned or not.

as you can see in Java Doc :

This interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.

you can throw same exception if you don't want this feature!!

Prashant
  • 2,556
  • 2
  • 20
  • 26