我有以下代码:
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