Java中的问题,求高手解答下!finally语句块为什么执行3次?

import java.io.*;
public class testExceptionProc
{
public static void main(String[] args)
{

try
{
int reader;
System.out.println("请输入一个数字(输入0为退出): ");
//reader!=0 循环开始 48表示ASCLL码中的0
label:
while((reader=System.in.read())!=48)
{
process(reader);
continue label;
}
}
catch(IOException g)
{
System.out.println(g.toString());
}

}

public static void process(int a)
{
try
{
if(a==49) //ASCLL码的49这里的1
{
System.out.println("没有任何异常出现!");
}
else if(a==50)//ASCLL码的49这里的2
{

int x=1,y=0;
int result=x/y;
}
else if(a==51)//ASCLL码的49这里的3
{
int []number={1,2,3};
int number1=number[3];
}
}
catch(ArithmeticException e)
{
System.out.println("异常!分母不能为零!");
}
catch(ArrayIndexOutOfBoundsException f)
{
System.out.println("异常!数组下标越界!");
}
catch(Exception one)
{
System.out.println(one.toString());
}
finally
{
System.out.println("finally语句得到执行!");
}
}
}

第1个回答  2011-10-18
while((reader=System.in.read())!=48)
{
process(reader);
continue label;
}
只有你的输入为0时才结束,所以在0之前你输入多少个字符,就执行多少次process方法,所以finally也就执行了多次。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网