我无法使用try和catch语句包含以下if语句。我仍然看到ArrayIndexOutOfBoundsException
错误。
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
int i = sc.nextint();
int w = sc.nextint();
try {
if(i == 2 && w == 3)
{
System.out.println(“Error. Please enter the missing elements.”);
}
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(“Error. Please try again.”);
}
答案 0 :(得分:2)
你在if语句中没有对数组做任何事情。在其他地方引起异常。
答案 1 :(得分:1)
抛出的ArrayIndexOutOfBoundsException
异常是您必须纠正的程序中的错误。抓住也不例外
您永远不应该引用数组边界之外的索引。
无论如何,如上所述,这段代码不会抛出任何ArrayIndexOutOfBoundsException
。