Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java try catch不处理IndexOutOfBoundsException_Java_List_Indexoutofboundsexception - Fatal编程技术网

Java try catch不处理IndexOutOfBoundsException

Java try catch不处理IndexOutOfBoundsException,java,list,indexoutofboundsexception,Java,List,Indexoutofboundsexception,在Java中尝试捕获列表的IndexOutOfBoundsException时遇到了一些问题。因此,我用两个元素声明了我的列表: List<String> list = new ArrayList<>(Arrays.asList("item1", "item2")); List List=newarraylist(Arrays.asList(“item1”、“item2”); 然后我试着做一个试捕: do { for (int i = 0; i < li

在Java中尝试捕获列表的IndexOutOfBoundsException时遇到了一些问题。因此,我用两个元素声明了我的列表:

List<String> list = new ArrayList<>(Arrays.asList("item1", "item2"));
List List=newarraylist(Arrays.asList(“item1”、“item2”);
然后我试着做一个试捕:

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);
do{
对于(int i=0;i
然而,当我输入任何大于2的选项时,它抛出了IndexOutOfBounds异常,但我认为我已经尝试捕捉了它


提前感谢。

如果不使用无效值调用列表,您将无法捕获异常

    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();
ArrayList list=新的ArrayList(Arrays.asList(“item1”、“item2”);
扫描仪sc=新的扫描仪(System.in);
int选项;
试一试{
option=sc.nextInt();
System.out.println(list.get(option));
}catch(IndexOutOfBoundsException e){
System.out.println(“无效选项”);
}捕获(输入不匹配异常e){
System.out.println(“选项输入不匹配”);
}
sc.close();
do{
对于(int i=0;i
我在type=list.get((选项-1))添加了新的try-catch;
要强制用户重新输入选项,我将在catch cause处将选项设置为3。您也可以这样做,它将循环,直到您输入有效值,输入有效值后,询问名称或其他内容(未实现)

ArrayList list=新的ArrayList(Arrays.asList(“item1”、“item2”);
扫描仪sc=新的扫描仪(System.in);
int选项=0;
而(!(选项==1 | |选项==2)){
试一试{
option=sc.nextInt();
}捕获(输入不匹配异常e){
System.out.println(“选项输入不匹配”);
}
}
System.out.println(list.get(选项1));
sc.close();

您在发布的代码中没有使用
列表
做任何事情?为什么
nextInt
会抛出
IndexOutOfBoundsException
?请包括实际抛出异常的代码。如果异常来自您的
列表
,则
try
块必须包含使用您的
列表
的代码才能捕获异常。它可能来自其他地方,请显示堆栈跟踪为什么要以这种方式实例化列表?您可以简化行列表itemList=Arrays.asList(“item1”、“item2”);我发现问题来自这一行:type=list.get((选项-1));。在我用try-catch将其包围之后,错误消失了,但问题是,当用户输入任何大于2的内容时,它不会再次提示输入,这就是我的想法,但它不会提示用户再次输入。正如我输入的大于2一样,该部分的第一个for循环不会再次打印并要求输入控制台中有什么?只是一个空白,我必须输入任何值,然后该部分的for循环将在之后出现。啊,好吧,我已经知道了,因为上次try catch中的sc.next(),这就是为什么您需要在控制台中添加一些提示,例如System.out.print(“重新输入选项:”);
    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);
    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();