Denys_newbie
Denys_newbie

Reputation: 1160

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
{

}

Upvotes: 1

Views: 68

Answers (1)

Federico klez Culloca
Federico klez Culloca

Reputation: 27139

Because the error is at compile time.

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

Upvotes: 7

Related Questions