电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

一个关于抛出异常的程序代码的分析


发布日期:2020/10/7
 

public class test {

public static string output =

public static void foo(int i) {

try {

if(i= =) {

throw new Exception ();

}

output += ;

)

catch(Exception e) {

output += ;

return;

)

finally (

output += ;

)

output += ;

)

public static void main (string args[]) (

foo();

foo();

)

)

What is the value of the variable output at line ?

Ans:

答案怎么会输出个数字呢?

首先foo()就直接到第output=

然后因为没有抛出异常所以直接运行

然后运行此时output=

foo()因为抛出异常所以运行

然后因为catch了异常然后运行finally里的语句

但是行的不再被运行因为行已经要求return了

上一篇:Scala 2.10和2.9.2的性能比较

下一篇:Swing写的简单的计算器