public class a15
{
public static void main(String[] args)
{
int arr[]={4,12,87,21,6,18};
int den[]={2,0,7,0,61,0};
double sum=0.0;
for(int i=0;i<=6;i++)
{
/*if((den[i]==0)||(i>den.length-1))
{
System.out.println("程序执行有误");
break;
}
else
{
sum+=(double)arr[i]/den[i];
System.out.println("sum="+sum);
}*/
try
{
sum+=(double)arr[i]/den[i];
System.out.println("sum="+sum);
}
catch(ArithmeticException e)
{
System.out.println("除数为0");
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("超出数组大小");
}
}
System.out.println("Hello World!");
}
}
在网上偶然看到的程序,为什么除数为0时,没有抛出异常,自己没有想明白??各位大虾看看吧 |