{"id":40340,"date":"2024-11-26T17:29:09","date_gmt":"2024-11-26T09:29:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40340\/"},"modified":"2024-11-26T17:29:09","modified_gmt":"2024-11-26T09:29:09","slug":"reasons-why-the-finally-block-may-not-execute-in-java","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40340\/","title":{"rendered":"Reasons Why the finally Block May Not Execute in Java"},"content":{"rendered":"<h2> 1. \u7406\u89e3finally\u5757\u7684\u76ee\u7684 <\/h2>\n<p>finally \u5757\u901a\u5e38\u7528\u4e8e\u8d44\u6e90\u6e05\u7406\uff0c\u4f8b\u5982\u5173\u95ed\u6587\u4ef6\u3001\u91ca\u653e\u9501\u6216\u7ec8\u6b62\u8fde\u63a5\u3002\u65e0\u8bba try \u6216 catch \u5757\u4e2d\u53d1\u751f\u4ec0\u4e48\uff0cfinally \u4e2d\u7684\u4ee3\u7801\u90fd\u5e94\u8be5\u6267\u884c\uff0c\u8fd9\u4f7f\u5176\u6210\u4e3a\u786e\u4fdd\u8d44\u6e90\u5f97\u5230\u6b63\u786e\u7ba1\u7406\u7684\u9996\u9009\u3002 <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173242928272295.jpg\" class=\"aligncenter\" title=\"Reasons Why the finally Block May Not Execute in Java\u63d2\u56fe\" alt=\"Reasons Why the finally Block May Not Execute in Java\u63d2\u56fe\" \/><\/p>\n<h3> 1.1finally\u7684\u57fa\u672c\u7528\u6cd5 <\/h3>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u8bf4\u660e\u4e86 finally \u5757\u7684\u4f7f\u7528\uff1a<\/p>\n<pre>public class finallyexample {\n    public static void main(string[] args) {\n        try {\n            int result = 10 \/ 0; \/\/ this will cause an arithmeticexception\n        } catch (arithmeticexception e) {\n            system.out.println(\"exception caught: \" + e.getmessage());\n        } finally {\n            system.out.println(\"this block will always execute.\");\n        }\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>exception caught: \/ by zero\nthis block will always execute.\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u65e0\u8bba\u629b\u51fa\u548c\u6355\u83b7\u5f02\u5e38\uff0cfinally \u5757\u90fd\u4f1a\u8fd0\u884c\uff0c\u8fd9\u662f\u9884\u671f\u7684\u884c\u4e3a\u3002 <\/p>\n<h3> 1.2 \u6700\u540e\u7684\u91cd\u8981\u6027 <\/h3>\n<p><strong>finally<\/strong> \u7684\u4e3b\u8981\u4f5c\u7528\u662f\u786e\u4fdd\u65e0\u8bba <strong>try-catch<\/strong> \u5757\u4e2d\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\uff0c\u5173\u952e\u4ee3\u7801\u90fd\u4f1a\u8fd0\u884c\u3002\u4f8b\u5982\uff0c\u5728\u9700\u8981\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u6216\u6587\u4ef6\u6d41\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u7684\u60c5\u51b5\u4e0b\uff0c<strong>finally<\/strong> \u5757\u5c31\u53d8\u5f97\u81f3\u5173\u91cd\u8981\u3002 <\/p>\n<h2> 2. \u6700\u7ec8\u53ef\u80fd\u65e0\u6cd5\u6267\u884c\u7684\u573a\u666f <\/h2>\n<p>\u867d\u7136finally\u5757\u88ab\u8bbe\u8ba1\u4e3a\u5728try\u548ccatch\u5757\u4e4b\u540e\u6267\u884c\uff0c\u4f46\u5728\u6781\u5c11\u6570\u4f46\u91cd\u8981\u7684\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u3002\u8fd9\u4e9b\u60c5\u51b5\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u3001\u4e8b\u52a1\u4e0d\u5b8c\u6574\u6216\u5176\u4ed6\u5173\u952e\u95ee\u9898\uff0c\u56e0\u6b64\u4e86\u89e3\u5b83\u4eec\u5bf9\u4e8e\u4efb\u4f55\u5f00\u53d1\u4eba\u5458\u90fd\u81f3\u5173\u91cd\u8981\u3002 <\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<h3> 2.1 jvm\u4f55\u65f6\u9000\u51fa <\/h3>\n<p><strong>finally<\/strong> \u5757\u53ef\u80fd\u65e0\u6cd5\u6267\u884c\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u662f jvm \u5728\u5230\u8fbe <strong>finally<\/strong> \u5757\u4e4b\u524d\u9000\u51fa\u3002\u5982\u679c\u7a0b\u5e8f\u5728 <strong>try<\/strong> \u6216 <strong>catch<\/strong> \u5757\u4e2d\u8c03\u7528 <strong>system.exit()<\/strong> \uff0c\u5c31\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u3002 <strong>system.exit()<\/strong> \u65b9\u6cd5\u7acb\u5373\u7ec8\u6b62 jvm\uff0c\u4ece\u800c\u963b\u6b62 <strong>finally<\/strong> \u5757\u6267\u884c\u3002 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public class finallynotexecuted {\n    public static void main(string[] args) {\n        try {\n            system.out.println(\"in try block\");\n            system.exit(0); \/\/ jvm will exit, skipping the finally block\n        } finally {\n            system.out.println(\"this will not execute\");\n        }\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>in try block\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6c38\u8fdc\u4e0d\u4f1a\u5230\u8fbe <strong>finally<\/strong> \u5757\uff0c\u56e0\u4e3a <strong>system.exit(0)<\/strong> \u8c03\u7528\u5728\u6267\u884c <strong>try<\/strong> \u5757\u540e\u7acb\u5373\u7ec8\u6b62 jvm\u3002 <\/p>\n<h3> 2.2 \u5f53\u7ebf\u7a0b\u88ab\u6740\u6b7b\u6216\u4e2d\u65ad\u65f6 <\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173242928244366.jpg\" class=\"aligncenter\" title=\"Reasons Why the finally Block May Not Execute in Java\u63d2\u56fe1\" alt=\"Reasons Why the finally Block May Not Execute in Java\u63d2\u56fe1\" \/><\/p>\n<p>finally \u5757\u53ef\u80fd\u65e0\u6cd5\u8fd0\u884c\u7684\u53e6\u4e00\u79cd\u60c5\u51b5\u662f\uff0c\u6267\u884c try-catch-finally \u5757\u7684\u7ebf\u7a0b\u88ab\u7ec8\u6b62\u6216\u4ee5\u963b\u6b62\u6267\u884c finally \u5757\u7684\u65b9\u5f0f\u4e2d\u65ad\u3002\u8fd9\u53ef\u80fd\u53d1\u751f\u5728\u7ebf\u7a0b\u88ab\u5f3a\u5236\u7ec8\u6b62\u7684\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u3002 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public class finallyinterrupted {\n    public static void main(string[] args) {\n        thread thread = new thread(() -&gt; {\n            try {\n                system.out.println(\"thread running\");\n                \/\/ simulate long-running task\n                thread.sleep(1000);\n            } catch (interruptedexception e) {\n                system.out.println(\"thread interrupted\");\n            } finally {\n                system.out.println(\"this may not execute if thread is killed\");\n            }\n        });\n\n        thread.start();\n        thread.interrupt(); \/\/ interrupting the thread\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>thread running\nthread interrupted\nthis may not execute if thread is killed\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u7ebf\u7a0b\u5728\u5173\u952e\u65f6\u523b\u88ab\u4e2d\u65ad\u6216\u6740\u6b7b\uff0cfinally \u5757\u53ef\u80fd\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u4ece\u800c\u5bfc\u81f4\u4ee3\u7801\u51fa\u73b0\u6f5c\u5728\u95ee\u9898\u3002 <\/p>\n<h3> 2.3 \u5f53finally\u5757\u5305\u542b\u65e0\u9650\u5faa\u73af\u6216\u5f02\u5e38\u65f6 <\/h3>\n<p>\u6709\u8da3\u7684\u662f\uff0c\u5982\u679cfinally\u5757\u672c\u8eab\u5305\u542b\u5bfc\u81f4\u65e0\u9650\u5faa\u73af\u7684\u4ee3\u7801\u6216\u5f15\u53d1\u5757\u5185\u672a\u6355\u83b7\u7684\u5f02\u5e38\uff0c\u5219\u5b83\u53ef\u4ee5\u963b\u6b62finally\u5757\u5b8c\u6210\u5176\u6267\u884c\uff0c\u4ece\u800c\u6709\u6548\u5730\u8df3\u8fc7\u9884\u671f\u7684\u6e05\u7406\u3002 <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173242928269660.jpg\" class=\"aligncenter\" title=\"Reasons Why the finally Block May Not Execute in Java\u63d2\u56fe2\" alt=\"Reasons Why the finally Block May Not Execute in Java\u63d2\u56fe2\" \/><\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public class finallywithloop {\n    public static void main(string[] args) {\n        try {\n            system.out.println(\"in try block\");\n        } finally {\n            while (true) {\n                system.out.println(\"infinite loop in finally block\");\n                break; \/\/ breaking to avoid actual infinite loop\n            }\n        }\n        system.out.println(\"this will not be reached if the loop is infinite\");\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>In try block\nInfinite loop in finally block\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u867d\u7136\u5728\u6b64\u793a\u4f8b\u4e2d\u6545\u610f\u4e2d\u65ad\u5faa\u73af\uff0c\u4f46\u771f\u6b63\u7684\u65e0\u9650\u5faa\u73af\u4f1a\u963b\u6b62finally \u5757\u5b8c\u6210\uff0c\u5e76\u4e14\u540e\u7eed\u4ee3\u7801\u5c06\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\u3002 <\/p>\n<h2> \u4e09\u3001\u7ed3\u8bba <\/h2>\n<p>\u4e86\u89e3 <strong>finally<\/strong> \u5757\u4f55\u65f6\u4e0d\u6267\u884c\u5bf9\u4e8e\u7f16\u5199\u5f39\u6027\u4e14\u53ef\u9760\u7684 java \u5e94\u7528\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\u3002\u4e3b\u8981\u573a\u666f\u5305\u62ec\u901a\u8fc7 <strong>system.exit()<\/strong> \u7ec8\u6b62 jvm\u3001\u7ebf\u7a0b\u4e2d\u65ad\u6216\u7ec8\u6b62\uff0c\u4ee5\u53ca <strong>finally<\/strong> \u5757\u672c\u8eab\u5185\u7684\u95ee\u9898\uff0c\u4f8b\u5982\u65e0\u9650\u5faa\u73af\u6216\u672a\u6355\u83b7\u7684\u5f02\u5e38\u3002\u901a\u8fc7\u4e86\u89e3\u8fd9\u4e9b\u60c5\u51b5\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u8d44\u6e90\u5e76\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u5373\u4f7f\u5728\u590d\u6742\u7684\u60c5\u51b5\u4e0b\u4e5f\u80fd\u6309\u9884\u671f\u8fd0\u884c\u3002 <\/p>\n<p>\u5982\u679c\u60a8\u5bf9\u672c\u6587\u8ba8\u8bba\u7684\u4efb\u4f55\u8981\u70b9\u6709\u4efb\u4f55\u7591\u95ee\u6216\u9700\u8981\u8fdb\u4e00\u6b65\u6f84\u6e05\uff0c\u8bf7\u968f\u65f6\u5728\u4e0b\u9762\u53d1\u8868\u8bc4\u8bba\u3002 <\/p>\n<p><strong>\u9605\u8bfb\u66f4\u591a\u5e16\u5b50<\/strong>\uff1afinally \u5757\u5728 java \u4e2d\u53ef\u80fd\u65e0\u6cd5\u6267\u884c\u7684\u539f\u56e0<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fReasons Why the finally Block May Not Execute in Java\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u7406\u89e3finally\u5757\u7684\u76ee\u7684 finally \u5757\u901a\u5e38\u7528\u4e8e\u8d44\u6e90\u6e05\u7406\uff0c\u4f8b\u5982\u5173\u95ed\u6587\u4ef6\u3001\u91ca\u653e\u9501\u6216\u7ec8\u6b62\u8fde\u63a5\u3002\u65e0\u8bba try \u6216 catch \u5757\u4e2d\u53d1\u751f\u4ec0\u4e48\uff0cfinally \u4e2d\u7684\u4ee3\u7801\u90fd\u5e94\u8be5\u6267\u884c\uff0c\u8fd9\u4f7f\u5176\u6210\u4e3a\u786e\u4fdd\u8d44\u6e90\u5f97\u5230\u6b63\u786e\u7ba1\u7406\u7684\u9996\u9009\u3002 1.1finally\u7684\u57fa\u672c\u7528\u6cd5 \u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u8bf4\u660e\u4e86 finally \u5757\u7684\u4f7f\u7528\uff1a public class finallyexample { public static void main(string[] args) { try { int result = 10 \/ 0; \/\/ this will cause an arithmeticexception } catch (arithmeticexception e) { system.out.println(&#8220;exception caught: &#8221; + e.getmessage()); } finally { system.out.println(&#8220;this block will always execute.&#8221;); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-40340","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=40340"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40340\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}