选择题
1. 下列程序的输出结果是______。 public class Test { public static void main(String[] args){ int[] array={2,4,6,8,10}; int size=6; int result=-1; try { for(int i=0;i<size && result==-1;i++) if(array[i]==20) result=i; } catch(ArithmeticException e){ System.out.println("Catch---1"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Catch---2"); } catch(Exception e){ System.out.println("Catch---3"); } } }
查看答案与解析
本题答案为B。解释:循环在i取0至4时均未找到20;i取5时访问array[5]超出长度为5的数组范围,抛出ArrayIndexOutOfBoundsException,由第二个catch捕获并输出Catch---2。
