1

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
{

}
Denys_newbie
  • 1,140
  • 5
  • 15

1 Answers1

7

Because the error is at compile time.

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

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95