下列程序的运行结果是( )。 publiccIasstest{ privateString[]data={“10”,”10.5”}; publicvoidfun( ){ doubles=0l for(inti=0}i

admin2013-12-02  32

问题 下列程序的运行结果是(    )。
publiccIasstest{
privateString[]data={“10”,”10.5”};
publicvoidfun(    ){
doubles=0l
for(inti=0}i<3Ii++){
try{
s=s+Integer.parseInt(data);
catch(Exceptione){
System.Out.print(”errorl:”+data);
}
}
}
publicstaticvoidmain(string[]args){
try{
testd=newtest(    );
d.fun(    );
}catch(Exceptione){
System.out.println(“error2”)
}
}
}

选项 A、errorlll0.5
B、error2
C、errorl:10.5error2
D、以上都不对

答案C

解析 try—catch块是可以嵌套分层的.并且通过异常对象的数据类型来进行匹配,以找到正确的catchblock异常错误处理代码。以下是通过异常对象的数据类型来进行匹配找到正确的catchblock的过程。①首先在抛出异常的try—catch块中查找catchblock,按顺序先与第一个catchblock块匹配,如果抛出的异常对象的数据类型与catchblock中传入的异常对象的临时变量(就是catch语句后面参数)的数据类型完全相同,或是它的子类型对象,则匹配成功,进入到catchblock中执行t否则到第2步;②如果有两个或更多的catchblock,则继续查找匹配第二个、第三个,直至最后一个catchblock,如匹配成功,则进入到对应的catchblock中执行,否则到第3步;③返回到上一级的try—catch块中.按规则继续查找对应的catchblock。如果找到,进入到对应的catchblock中执行,否则到第4步;④再到上上级的try—catch块中,如此不断递归,直到匹配到顶级的try—catch块中的最后一catchblock,如果找到,进入到对应的catchblock中执行l否则程序将会执行terminate()退出。所以本题选C。
转载请注明原文地址:https://kaotiyun.com/show/ymID777K
0

相关试题推荐
最新回复(0)