问题 单项选择题

下面程序的输出结果是
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");
  
 

A) Catch---1 B) Catch一--2 C) Catch---3 D) 以下都不对

答案

参考答案:B

解析: ArithmeticException是当出现异常的运算条件时,抛出此异常。例如,一个整数“除以零”时,是抛出此类的一个实例;ArrayIndexOutOfBoundsException是用非法索引访问数组时抛出的异常。如果索引为负或大于等于数组大小,则该索引为非法索引。本程序中,数组大小为5个元素,数组下标为0~4,而循环为0~5,数组越界,所以会捕获ArrayIndexOutOfBoundsException。

单项选择题
填空题