java异常捕获问题,从命令行输入数据

从命令行得到5个整数并存入一个整型数组,每两个数之间隔一个空格输出。如果命令行输入的数据不是整数,要求捕获Integer.parseInt()方法产生的异常(数字格式异常NumberFormatException),并提示“请输入整数”;如果输入的参数不足5个,要求捕获数组下标越界异常(ArrayIndexOutOfBoundsException),并提示“请输入至少5个整数”。求大神指点



```java
package homework.Account;

import java.util.Scanner;

public class ArrayException {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] arr = new int[5];
        String numStr = input.nextLine();
        String[] arrStr1 = numStr.split(" ");
        for (int i = 0; i < 5;i++){

            try {
                arr[i] = Integer.parseInt(arrStr1[i]);
            }catch (NumberFormatException e){
                System.out.println("请输入整数");
                break;
            }catch (ArrayIndexOutOfBoundsException e){
                System.out.println("请输入至少5个整数");
                break;
            }

        }
    for (int v:arr){
        System.out.print(v+" ");
    }

    }
}





```

try{
int val = Integer.parse(arg[i]); //i=1,2...,5
}catch (NumberFormatException ex){
sysout("参数格式异常");
}catch (ArrayIndexOutOfBoundsException ex2){
sysout("参数不能小于5个");
}catch (Exception ex3){
sysout ("其它异常");
}

大概是这样子,你自己完善吧……

来俩个catch
每个catch里面捕获一个异常,异常体里面来个控制台打印

public class Test{
 public static void main(String[] args) throws Exception {
    if(args.length < 5){
        throw new ArrayIndexOutOfBoundsException("请输入至少5个整数");
    }
    int count = 5;
    int[] arr = new int[count];
    for(int i=0; i<count; i++){
        try{
            arr[i] = Integer.parseInt(args[i]);
        }catch(Exception e){
            throw new NumberFormatException("请输入整数");
        }
    }
}
}
public static void main(String[] args) throws Exception {
    if(args.length < 5){
        throw new ArrayIndexOutOfBoundsException("请输入至少5个整数");
    }
    int count = 5;
    int[] arr = new int[count];
    for(int i=0; i<count; i++){
        try{
            arr[i] = Integer.parseInt(args[i]);
        }catch(Exception e){
            throw new NumberFormatException("请输入整数");
        }
    }
}

public static void main(String[] args) {

    /*
     * 从命令行得到5个整数,放入一个整型数组,然后打印输出 //要求:如果输入数据不为整数
     * 要捕获Integer.parseInt()产生的异常,显示“请输入整数”, 捕获输入数参数不足5个的异常(数组越界),
     * 显示“请输入至少5个整数”。
     */
    Scanner sc = new Scanner(System.in);
    int[] a = new int[5];
    System.out.println("请输入");
    for (int i = 0; i < 5; i++) {
        try {
            String aa = sc.next();
            a[i]=Integer.parseInt(aa);
        } catch (NumberFormatException e) {
            System.out.println("请输入整数");
        }catch(ArrayIndexOutOfBoundsException e){
               System.out.println("请输入5个整数");
          }finally{
              sc.close();
          }
    }
    System.out.println(Arrays.toString(a));

}