【发布时间】:2013-09-10 05:42:30
【问题描述】:
我有以下代码:
public class test3
{
public static void main(String a[])
{
System.out.println("In main");
System.out.println(new Demo(0).devide());
}
}
class Demo
{
int x;
Demo(int x)
{
this.x=x;
}
public int devide()
{
try
{
return x/x;
}
catch(Exception e)
{
System.out.println(e);
return 4;
}
finally
{
System.out.println("In finally");
return 1;
}
}
}
在上面的代码中,我期望 4 作为输出,但生成的输出是:
In main
java.lang.ArithmeticException: / by zero
In finally
1
所以它返回1
【问题讨论】:
-
这正是这个问题中提出的问题:stackoverflow.com/questions/15225819/…
-
阅读这个similar question,它可能会有所帮助