我收到错误:无法抛出 ArrayIndexOutOfBoundsException 类型的异常;异常类型必须是 Throwable 类的子类型

发布于 2025-01-11 11:14:31 字数 958 浏览 0 评论 0原文

import java.util.*;

public class ArrayIndexOutOfBoundsException {
    public static void main(String[] args) {
        int[] array = new int[100];  

//创建100个存储空间的数组 for(int i = 0; i < array.length; i++) { //for循环在数组的每个索引中存储随机整数 array[i] = (int) (Math.random()*100); }

        Scanner input = new Scanner(System.in);
        System.out.println("Enter the index of the array: "); 

//提示用户输入索引进行查找

        try {
            int index = input.nextInt(); //declaring index variable to take on inputed value
            System.out.println("The integer at index "+index+" is: "+array[index]); //printing the integer at the specified index
        
        }
        catch (ArrayIndexOutOfBoundsException ex) { //if user enters index value outside of 0-99, exception message will print
            System.out.println("Out of bounds.");
        }

        
    } 
        
}
import java.util.*;

public class ArrayIndexOutOfBoundsException {
    public static void main(String[] args) {
        int[] array = new int[100];  

//creating array with 100 storage spaces
for(int i = 0; i < array.length; i++) { //for loop to store random integers in each index of the array
array[i] = (int) (Math.random()*100);
}

        Scanner input = new Scanner(System.in);
        System.out.println("Enter the index of the array: "); 

//prompting user to enter index to find

        try {
            int index = input.nextInt(); //declaring index variable to take on inputed value
            System.out.println("The integer at index "+index+" is: "+array[index]); //printing the integer at the specified index
        
        }
        catch (ArrayIndexOutOfBoundsException ex) { //if user enters index value outside of 0-99, exception message will print
            System.out.println("Out of bounds.");
        }

        
    } 
        
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你列表最软的妹 2025-01-18 11:14:31

当您的代码被编译为字节代码时,编译器必须发现所有类并将所有名称扩展为它们的 FQDN - 包 + 类名

在您的情况下,当编译程序时,主类名是 ArrayIndexOutOfBoundsException - 因此编译器将 ArrayIndexOutOfBoundsException 映射到您自己的类。

当编译器捕获该行时,它会获取 ArrayIndexOutOfBoundsException 并尝试首先在映射中找到它 - 它就在那里。因此编译器开始检查正确性,特别是该类必须位于 Throwable 层次结构中。由于它不在可抛出层次结构中(您的类隐式扩展了 Object),因此编译器会返回错误。

您可以使用两种方法修复它:

  1. 重命名主类以避免
  2. catch 中的歧义 您可以指定类的全名: java.lang.ArrayIndexOutOfBoundsException

第二个选项有助于解决一般问题:如果两个类具有相同的名称,但是怎么办必须在同一范围内使用。

When your code is being compiled to the byte code, the compiler has to discover all classes and extend all names into their FQDNs - packages + class name

In your case, when the programs is compiled, the main class name is ArrayIndexOutOfBoundsException - so the compiler maps ArrayIndexOutOfBoundsException to your own class.

When compiler gets to catch line, it takes ArrayIndexOutOfBoundsException and tries to first locate it in the map - and it is there. So the compilers starts checking the correctness, in particular, the class has to be in Throwable hierarchy. Since it is not in throwable hierarchy (your class implicitly extends Object), the compiler returns an error.

You could fix it using two ways:

  1. rename you main class to avoid ambiguity
  2. in catch you may specify full name for the class: java.lang.ArrayIndexOutOfBoundsException

The second options helps with a generic problem: what if two classes have the same name, but have to be used in the same scope.

九局 2025-01-18 11:14:31

ArrayIndexOutOfBoundsException 异常类型包含在 java/lang 包中。因此,您必须导入它或在 catch 子句中使用全名:

catch (java.lang.ArrayIndexOutOfBoundsException ex)

在您的情况下,导入将不起作用,因为您的类也称为 ArrayIndexOutOfBoundsException 所以您需要在 catch 子句中使用全名。

作为最后的建议,我建议您将类重命名为一个好的实践,因为它可能会导致混乱并使代码难以阅读。

The ArrayIndexOutOfBoundsException exception type is included in the java/lang package. So you have to import it or use the full name in your catch clause:

catch (java.lang.ArrayIndexOutOfBoundsException ex)

In your situation, import won't work because your class is also called ArrayIndexOutOfBoundsException so you'll need to use the full name in the catch clause.

As a final advice, I recommend you rename your class as a good practice cause as it is now, it could lead to confusion and turn the code difficult to read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文