Guide to ArrayStoreException – ArrayStoreException指南

最后修改: 2020年 8月 31日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

ArrayStoreException is thrown at runtime in Java when an attempt is made to store the incorrect type of object in an array of objects. Since ArrayStoreException is an unchecked exception, it isn’t typical to handle or declare it.

ArrayStoreException是在Java中运行时抛出的当试图在对象的数组中存储不正确的对象类型时。由于ArrayStoreException是一个未经检查的异常,所以处理或声明它并不典型。

In this tutorial, we’ll demonstrate the cause of ArrayStoreException, how to handle it, and best practices for avoiding it.

在本教程中,我们将展示ArrayStoreException的原因,如何处理它,以及避免它的最佳做法。

2. Causes of ArrayStoreException

2、ArrayStoreException的原因

Java throws an ArrayStoreException when we try to store a different type of object in an array instead of the declared type.

当我们试图在数组中存储不同类型的对象而不是声明的类型时,Java会抛出一个ArrayStoreException

Suppose we instantiated an array with String type and later tried to store Integer in it. In this case, during runtime, ArrayStoreException is thrown:

假设我们实例化了一个String类型的数组,后来试图在其中存储Integer。在这种情况下,在运行时,ArrayStoreException会被抛出。

Object array[] = new String[5];
array[0] = 2;

The exception will be thrown at the second line of code when we try to store an incorrect value type in the array:

当我们试图在数组中存储一个不正确的值类型时,异常将在第二行代码中抛出。

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
    at com.baeldung.array.arraystoreexception.ArrayStoreExceptionExample.main(ArrayStoreExceptionExample.java:9)

Since we declared array as an Object, the compilation is error-free.

由于我们将array声明为Object编译没有错误

3. Handling the ArrayStoreException

3.处理ArrayStoreException

The handling of this exception is pretty straightforward. As any other exception, it also needs to be surrounded in a try-catch block for handling:

对这个异常的处理是非常直接的。和其他的异常一样,它也需要被包围在try-catch块中进行处理。

try{
    Object array[] = new String[5];
    array[0] = 2;
}
catch (ArrayStoreException e) {
    // handle the exception
}

4. Best Practices to Avoid This Exception

4.避免这种异常情况的最佳做法

It is recommended to declare the array type as a specific class, such as String or Integer, instead of Object. When we declare the array type as Object, then the compiler will not throw any error.

建议将数组类型声明为一个特定的类,如StringInteger,而不是Object。当我们将数组类型声明为Object,时,编译器将不会产生任何错误。

But declaring the array with the base class and then storing objects of a different class will lead to a compilation error. Let’s see this with a quick example:

但是用基类声明数组,然后存储不同类的对象会导致编译错误。让我们通过一个简单的例子来看看这个问题。

String array[] = new String[5];
array[0] = 2;

In the above example, we declare the array type as String and try to store an Integer in it. This will lead to a compilation error:

在上面的例子中,我们将数组类型声明为String,并试图在其中存储一个Integer。这将导致一个编译错误。

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
  Type mismatch: cannot convert from int to String
    at com.baeldung.arraystoreexception.ArrayStoreExampleCE.main(ArrayStoreExampleCE.java:8)

It’s better if we catch errors at compile-time rather than runtime as we have more control over the former.

如果我们在编译时而不是在运行时捕获错误会更好,因为我们对前者有更多控制。

5. Conclusion

5.结论

In this tutorial, we learned the causes, handling, and prevention of ArrayStoreException in Java.

在本教程中,我们学习了Java中ArrayStoreException的原因、处理和预防。

The complete example is available over on GitHub.

完整的例子可在GitHub上找到。