为什么ArrayStoreException是RuntimeException? [英] Why is the ArrayStoreException a RuntimeException?

查看:119
本文介绍了为什么ArrayStoreException是RuntimeException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下程序:

class Fruit {}

class Apple extends Fruit {} 

class Jonathan extends Apple {} 

class Orange extends Fruit {} 

public class Main { 
    public static void main(String[] args) { 
        Fruit[] fruit = new Apple[10];

        try { 
            fruit[0] = new Fruit(); // ArrayStoreException 
            fruit[0] = new Orange(); // ArrayStoreException 
        } catch(Exception e) { System.out.println(e); } 
    } 
}

基于 Java文档


抛出该错误表示试图将错误的
类型的对象存储到对象数组中。

Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.

我已阅读此处 b
$ b

I've read here that


创建数组时,它会记住要存储的数据类型。

When array is created it remembers what type of data it is meant to store.

如果数组记住了它包含的数据类型,则意味着它知道它包含的数据类型。但是我发布的代码段已正确编译,因此在编译时,数组显然不知道所包含的类型。

If the array remembers what type of data it contains, it means that it KNEW the type of data it contains. But the snippet I posted is correctly compiled, so at compile time the array apparently doesn't know what type contains.

我的问题是:


  1. 为什么仅在运行时抛出 ArrayStoreException

编译器缺少哪些信息以致无法进行分配?

What information are missing to the compiler to realise that that assignment is not possible?


推荐答案


创建数组时,它会记住
存储意味着什么类型的数据。

When array is created it remembers what type of data it is meant to store.

该数组仅记住运行时实际包含的类型。

The array "remembers" only what type it actually contains during runtime.

首先声明该数组,在这种情况下,声明为Fruit数组。

First the array is declared, in this case as an array of Fruit.

然后创建数组,在本例中为Apple数组。

Then the array is created, in this case as an array of Apple.

创建是在运行时创建的,但是编译器是设计的仅用于验证仅向数组分配了声明为该数组的对象。在运行期间可能发生很多事情。

Creation is made during runtime, but the compiler is designed only to verify that the array only is assigned objects of the type it is declared as. There are so many things that can occur during runtime.

请考虑以下代码:

class Fruit {}

class Apple extends Fruit {} 

class Jonathan extends Apple {} 

class Orange extends Fruit {} 

public class Main { 
    public static void main(String[] args) { 
        Fruit[] fruit = new Apple[10];
        boolean alt = (Math.random() < 0.5);

        try { 
            fruit[0] = fruitFactory(alt); 
        } catch(Exception e) { System.out.println(e); } 
    } 

    private static Fruit fruitFactory(boolean apple) {
        if (apple) {
            return new Apple();
        } else {
            return new Orange();
        }
    } 
}

代码与您的代码相同除了通过fruitFactory方法为fruit [0]分配了一个值。编译器无法判断布尔alt是 true 还是 false

The code is identical to your except that the fruit[0] is assigned a value by the fruitFactory method. There is no way the compiler can tell if the boolean alt is going to be true or false.


编译器缺少哪些信息,以致无法进行
赋值?

What information are missing to the compiler to realise that that assignment is not possible?

如上所述,

-编译器无法判断赋值是否可行。

As said - the compiler cannot tell if the assignment is possible or not.


在这样的代码正确的情况下,不会抛出
ArrayStoreException吗?

Is there any cases in which such code is correct so no ArrayStoreException is thrown?

是,在50%的情况下上面的代码。您要么必须验证分配的对象与数组相同,要么捕获异常。

Yes, in 50 % of the cases in the code above. You either have to verify that the object assigned is the same as the array or catch the exception.

这篇关于为什么ArrayStoreException是RuntimeException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆