{"id":204293,"date":"2025-05-29T10:47:36","date_gmt":"2025-05-29T02:47:36","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204293\/"},"modified":"2025-05-29T10:47:36","modified_gmt":"2025-05-29T02:47:36","slug":"python-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88-try-finally-%e4%bb%a3%e7%a0%81%e5%9d%97%e6%97%a0%e6%b3%95%e8%a7%a3%e5%86%b3%e5%88%97%e8%a1%a8%e5%85%83%e7%b4%a0%e5%88%a0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204293\/","title":{"rendered":"Python \u5f02\u5e38\u5904\u7406\uff1a\u4e3a\u4ec0\u4e48 try-finally \u4ee3\u7801\u5757\u65e0\u6cd5\u89e3\u51b3\u5217\u8868\u5143\u7d20\u5220\u9664\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5f02\u5e38\u5904\u7406\uff1a\u4e3a\u4ec0\u4e48 try-finally \u4ee3\u7801\u5757\u65e0\u6cd5\u89e3\u51b3\u5217\u8868\u5143\u7d20\u5220\u9664\u5f02\u5e38\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241103\/17306266086727443005ef9.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u5f02\u5e38\u5904\u7406\uff1a\u4e0d\u5e26 except \u62a5\u9519\u539f\u56e0<\/strong><\/p>\n<p>\u5728\u67d0\u6b21\u5b57\u7b26\u4f4d\u7f6e\u68c0\u6d4b\u4ee3\u7801\u4e2d\uff0c\u5f00\u53d1\u8005\u9047\u5230\u4e86\u4e00\u4e2a\u95ee\u9898\u3002\u8be5\u4ee3\u7801\u6b63\u5728\u5c1d\u8bd5\u5220\u9664\u5217\u8868 y \u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u5bfc\u81f4\u5f02\u5e38\u3002\u7136\u800c\uff0c\u4f7f\u7528 try \u548c finally \u4ee3\u7801\u5757\u5374\u65e0\u6cd5\u89e3\u51b3\u95ee\u9898\uff0c\u800c\u6dfb\u52a0 except \u5757\u5219\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<p>\u4ea7\u751f\u8fd9\u4e2a\u95ee\u9898\u7684\u4e3b\u8981\u539f\u56e0\u662f\uff1a<\/p>\n<ul>\n<li><strong>try-except \u673a\u5236\uff1a<\/strong>\u5982\u679c\u5728 try \u4ee3\u7801\u5757\u4e2d\u53d1\u751f\u5f02\u5e38\u5e76\u4e14\u6ca1\u6709\u5339\u914d\u7684 except \u5b50\u53e5\uff0c\u5f02\u5e38\u5c06\u88ab\u4f20\u9012\u5230\u4e0a\u5c42 try \u5757\uff08\u5982\u679c\u6709\uff09\uff0c\u76f4\u5230\u7a0b\u5e8f\u6700\u4e0a\u5c42\u3002\u7136\u540e\uff0c\u7a0b\u5e8f\u5c06\u7ec8\u6b62\u5e76\u6253\u5370\u9ed8\u8ba4\u9519\u8bef\u6d88\u606f\u3002<\/li>\n<li><strong>finally \u4ee3\u7801\u5757\uff1a<\/strong>\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0cfinally \u4ee3\u7801\u5757\u603b\u662f\u4f1a\u88ab\u6267\u884c\u3002<\/li>\n<\/ul>\n<p>\u5728\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d\uff0ctry \u4ee3\u7801\u5757\u4e2d\u7684\u95ee\u9898\u662f\uff0c\u5b83\u4f1a\u5c1d\u8bd5\u5220\u9664\u5217\u8868 y \u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u5f02\u5e38\u662f\u7531 remove() \u65b9\u6cd5\u4ea7\u751f\u7684\uff0c\u4f46\u6ca1\u6709\u76f8\u5e94\u7684 except \u5b50\u53e5\u6765\u5904\u7406\u5b83\u3002\u8fd9\u4f1a\u5bfc\u81f4\u5f02\u5e38\u88ab\u4f20\u9012\u5230\u7a0b\u5e8f\u7684\u6700\u9ad8\u5c42\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u4f7f\u7528 except \u5b50\u53e5\u89e3\u51b3\u95ee\u9898<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a except \u5b50\u53e5\u6765\u6355\u83b7\u7531 remove() \u65b9\u6cd5\u629b\u51fa\u7684\u5f02\u5e38\u3002\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\uff1a<\/p>\n<pre>except Exception:\n    pass<\/pre>\n<p>\u6b64 except \u5b50\u53e5\u5c06\u6355\u83b7\u4efb\u4f55\u7c7b\u578b\u7684\u5f02\u5e38\u5e76\u5ffd\u7565\u5b83\u3002\u901a\u8fc7\u5904\u7406\u5f02\u5e38\uff0c\u5f02\u5e38\u4e0d\u4f1a\u88ab\u4f20\u9012\u5230\u7a0b\u5e8f\u7684\u6700\u9ad8\u5c42\uff0c\u7a0b\u5e8f\u56e0\u6b64\u4e0d\u4f1a\u7ec8\u6b62\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u56e0\u6b64\uff0c\u4e0d\u5e26 except \u5b50\u53e5\u7684 try-finally \u5757\u65e0\u6cd5\u89e3\u51b3\u5f02\u5e38\u95ee\u9898\uff0c\u56e0\u4e3a\u5f02\u5e38\u5c06\u88ab\u4f20\u9012\u5230\u4e0a\u5c42\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u7ec8\u6b62\u3002\u6dfb\u52a0\u4e00\u4e2a except \u5b50\u53e5\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\uff0c\u4ece\u800c\u9632\u6b62\u7a0b\u5e8f\u7ec8\u6b62\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPython \u5f02\u5e38\u5904\u7406\uff1a\u4e3a\u4ec0\u4e48 try-finally \u4ee3\u7801\u5757\u65e0\u6cd5\u89e3\u51b3\u5217\u8868\u5143\u7d20\u5220\u9664\u5f02\u5e38\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5f02\u5e38\u5904\u7406\uff1a\u4e3a\u4ec0\u4e48 &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204293","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204293"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204293\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}