小编Apu*_*shi的帖子

为什么return在最终块中不遵守变量的值?

finally总是最后执行,因此该语句x = 3应该最后执行。但是,运行此代码时,返回的值为2。

为什么?

class Test {
    public static void main (String[] args) {
        System.out.println(fina());
    }

    public static int fina()
    {
        int x = 0;
        try {
            x = 1;
            int a = 10/0;
        }
        catch (Exception e)
        {
            x = 2;
            return x;
        }
        finally
        {
            x = 3;
        }
        return x;
    }
}
Run Code Online (Sandbox Code Playgroud)

java exception try-catch-finally

0
推荐指数
1
解决办法
74
查看次数

标签 统计

exception ×1

java ×1

try-catch-finally ×1