javacloneable

Why I have an error, but not an exception?


documentation

Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

Why I have an error

clone() has protected access in java.lang.Object

but not CloneNotSupportedException exception?

public class Test
{
    public static void main(String[] args)
    {
        Test2 c1 = new Test2();
        Test2 c2 = (Test2) c1.clone(); // error: clone() has protected access in java.lang.Object
    }
}

class Test2
{

}

Solution

  • Because the error is at compile time.

    Exception are at runtime. The program didn't even compile, so you didn't reach runtime.