美文网首页
return 语句执行后还会执行 finally 块吗

return 语句执行后还会执行 finally 块吗

作者: MHLEVEL | 来源:发表于2020-10-05 15:30 被阅读0次

一、return 之后 finally 块也能执行


测试代码

public static void main(String[] args) {
    // write your code here

        try {
            System.out.println("try...");
            System.exit(1);
        } finally {
            System.out.println("finally...");
        }
    }

测试结果

try...
finally...

Process finished with exit code 0

二、exit() 方法执行后就不会执行 finally 块了


测试代码

public static void main(String[] args) {
    // write your code here

        try {
            System.out.println("try...");
            System.exit(1);
        } finally {
            System.out.println("finally...");
        }
    }

测试结果

try...

Process finished with exit code 1

相关文章

网友评论

      本文标题:return 语句执行后还会执行 finally 块吗

      本文链接:https://www.haomeiwen.com/subject/pceauktx.html