Why I have an error, but not an exception?

68 views Asked by At

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
{

}
1

There are 1 answers

0
Federico klez Culloca On BEST ANSWER

Because the error is at compile time.

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