【问题标题】:Java try catch not handling IndexOutOfBoundsExceptionJava try catch 不处理 IndexOutOfBoundsException
【发布时间】:2023-03-03 01:54:01
【问题描述】:

我在尝试捕获 Java 中的 List 的 IndexOutOfBoundsException 时遇到了一些问题。所以我用 2 个元素声明了我的列表:

List<String> list = new ArrayList<>(Arrays.asList("item1", "item2"));

然后我尝试做一个try catch:

do {
    for (int i = 0; i < list.size(); i++) {
        System.out.print("(" + (i + 1) + ")" + list.get(i));
    }
    System.out.println(" ");

    try{
        option = sc.nextInt();
    } catch (IndexOutOfBoundsException e){
        System.out.println("Invalid option");
        sc.next();
        continue;
    } catch (InputMismatchException e) {
        System.out.println("Option input mismatch.");
        sc.next();
        continue;
    } 
    sc.nextLine();
    if (option == 1) {
        System.out.print("Enter name: ");
        // scanner takes in input
    } else if (option == 2) {
        System.out.print("Enter desc: ");
        // scanner takes in input
    }
type = list.get((option - 1));
} while (option <= 0 || option >= 3);

但是,当我输入任何大于 2 的选项时,它会抛出 IndexOutOfBounds 异常,但我认为我已经为它做了尝试捕获?

提前致谢。

【问题讨论】:

  • 您在发布的代码中没有使用list 做任何事情?
  • 为什么nextInt 会抛出IndexOutOfBoundsException?请包含实际抛出它的代码。
  • 如果异常来自您的list,那么try 块必须包含使用您的list 的代码才能捕获异常。
  • 可能来自其他地方,请显示堆栈跟踪
  • 你为什么要这样实例化列表?您可以简化该行 List itemList = Arrays.asList("item 1", "item2");

标签: java list indexoutofboundsexception


【解决方案1】:

你也可以这样做,它会循环直到你输入一个有效值,输入一个有效值后询问名称或其他(未实现)

    ArrayList<String> list = new ArrayList<>(Arrays.asList("item1", "item2"));
    Scanner sc = new Scanner(System.in);
    int option = 0;
    while(!(option == 1 || option==2) ) {

        try {
            option = sc.nextInt();              
        } catch (InputMismatchException e) {
            System.out.println("Option input mismatch.");
        }
    }
    System.out.println(list.get(option-1));
    sc.close();

【讨论】:

    【解决方案2】:
        do {
            for (int i = 0; i < list.size(); i++) {
                System.out.print("(" + (i + 1) + ")" + list.get(i));
            }
            System.out.println(" ");
    
            try {
                option = sc.nextInt();
            } catch (IndexOutOfBoundsException e) {
                System.out.println("Invalid option");
                sc.next();
                continue;
            } catch (InputMismatchException e) {
                System.out.println("Option input mismatch.");
                sc.next();
                continue;
            }
            sc.nextLine();
            if (option == 1) {
                System.out.print("Enter name: ");
                // scanner takes in input
            } else if (option == 2) {
                System.out.print("Enter desc: ");
                // scanner takes in input
            }
            try {
                type = list.get((option - 1));
            } catch (IndexOutOfBoundsException e) {
                System.out.println("Invalid option");
                option=3;
            }
        } while (option <= 0 || option >= 3);
    

    我在 type = list.get((option - 1)); 处添加了新的 try-catch 为了强制用户重新输入选项,我会在捕获原因时将选项设置为 3

    【讨论】:

    • 你好,这就是我的想法,但它不会提示用户再次输入输入。如在我输入大于 2 的情况下,第一个 for 循环该部分不会再次打印出来并要求输入
    • 你在控制台得到了什么?
    • 只是一个空白空间,我必须输入任何值,然后该部分的 for 循环将在此之后出现。啊,好吧,我已经知道了,因为上次尝试捕获中的 sc.next(),这就是原因
    • 你需要在控制台中放一些promt,例如System.out.print("Re-enter the option: ");
    【解决方案3】:

    如果不使用无效值调用列表,则不会捕获异常。

        ArrayList<String> list = new ArrayList<>(Arrays.asList("item1", "item2"));
        Scanner sc = new Scanner(System.in);
        int option;
        try {
            option = sc.nextInt();
            System.out.println(list.get(option));
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Invalid option");
        } catch (InputMismatchException e) {
            System.out.println("Option input mismatch.");
        }
    
        sc.close();
    

    【讨论】:

    • 我发现问题出在这一行:type = list.get((option - 1));。在我用 try catch 包围它之后,错误消失了,但问题是,当用户输入大于 2 的任何内容时,它不会再次提示输入
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 2016-09-13
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    相关资源
    最近更新 更多