为什么我有一个错误,而不是一个异常?

huangapple go评论57阅读模式
英文:

Why I have an error, but not an exception?

问题

文档

> 在不实现 Cloneable 接口的实例上调用 Object 的 clone 方法会导致抛出 CloneNotSupportedException 异常。

为什么会出现错误:

> clone() 在 java.lang.Object 中具有受保护的访问权限

但是没有抛出 CloneNotSupportedException 异常?

public class Test
{
    public static void main(String[] args)
    {
        Test2 c1 = new Test2();
        Test2 c2 = (Test2) c1.clone(); // 错误:clone() 在 java.lang.Object 中具有受保护的访问权限
    }
}

class Test2
{

}
英文:

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

得分: 7

因为错误出现在编译时。

异常是在运行时发生的。程序甚至没有编译通过,所以你没有达到运行时。

英文:

Because the error is at compile time.

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

huangapple
  • 本文由 发表于 2020年7月31日 01:02:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63177994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定