{"id":203096,"date":"2025-05-22T09:14:49","date_gmt":"2025-05-22T01:14:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203096\/"},"modified":"2025-05-22T09:14:49","modified_gmt":"2025-05-22T01:14:49","slug":"%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%ad%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e7%9a%84%e6%80%a7%e8%83%bd%e5%bd%b1%e5%93%8d%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203096\/","title":{"rendered":"\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f71\u54cd\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f71\u54cd\u662f\u4ec0\u4e48\uff1f<\/h1>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f71\u54cd\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u4e0d\u4f1a\u7ec8\u6b62\u7a0b\u5e8f\uff0c\u800c\u662f\u4f20\u64ad\u9519\u8bef\u503c\u3002\u6027\u80fd\u5f71\u54cd\u5305\u62ec\uff1a\u6267\u884c\u65f6\u95f4\u7565\u6709\u5f00\u9500\uff0c\u5185\u5b58\u4f7f\u7528\u589e\u52a0\uff0c\u5e76\u53d1\u65f6\u5f02\u5e38\u5904\u7406\u66f4\u590d\u6742\u3002\u5728\u9891\u7e41\u7684\u5f02\u5e38\u5904\u7406\u60c5\u51b5\u4e0b\uff0c\u6027\u80fd\u5f00\u9500\u4f1a\u5f88\u663e\u8457\uff0c\u9700\u8981\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u8981\u6c42\u9009\u62e9\u5408\u9002\u7684\u5f02\u5e38\u5904\u7406\u7b56\u7565\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730082952671ef88822a7e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f71\u54cd<\/strong><\/p>\n<p>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u901a\u5e38\u4f7f\u7528 <code>try-catch<\/code> \u7ed3\u6784\u3002\u4e0e\u547d\u4ee4\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u4e0d\u540c\uff0c\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u4e0d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u7ec8\u6b62\u3002\u76f8\u53cd\uff0c\u5b83\u4eec\u5c06\u9519\u8bef\u503c\u5411\u4e0a\u4f20\u64ad\uff0c\u76f4\u5230\u5b83\u4eec\u88ab\u6355\u83b7\u548c\u5904\u7406\u3002<\/p>\n<p><strong>\u6027\u80fd\u5f71\u54cd<\/strong><\/p>\n<p>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u4f1a\u5bf9\u7a0b\u5e8f\u6027\u80fd\u4ea7\u751f\u4ee5\u4e0b\u4e3b\u8981\u5f71\u54cd\uff1a<\/p>\n<ul>\n<li><strong>\u6267\u884c\u65f6\u95f4\uff1a<\/strong><code>try-catch<\/code> \u7ed3\u6784\u4f1a\u5bf9\u7a0b\u5e8f\u7684\u6267\u884c\u65f6\u95f4\u4ea7\u751f\u5c0f\u5e45\u5f00\u9500\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u4e3a\u6f5c\u5728\u7684\u5f02\u5e38\u68c0\u67e5\u5806\u6808\u3002<\/li>\n<li><strong>\u5185\u5b58\u4f7f\u7528\uff1a<\/strong>\u5f02\u5e38\u5904\u7406\u9700\u8981\u5b58\u50a8\u9519\u8bef\u4fe1\u606f\u548c\u5806\u6808\u8ddf\u8e2a\uff0c\u8fd9\u5c06\u589e\u52a0\u7a0b\u5e8f\u7684\u5185\u5b58\u6d88\u8017\u3002<\/li>\n<li><strong>\u5e76\u53d1\uff1a<\/strong>\u5728\u5e76\u53d1\u73af\u5883\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u4f1a\u53d8\u5f97\u66f4\u52a0\u590d\u6742\uff0c\u56e0\u4e3a\u9700\u8981\u8003\u8651\u4e0d\u540c\u7ebf\u7a0b\u4e2d\u5f02\u5e38\u7684\u4f20\u64ad\u548c\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u51fd\u6570\u5f0f\u4ee3\u7801\uff1a<\/p>\n<pre>def divide(numerator: int, denominator: int) -&gt; int:\n    try:\n        return numerator \/ denominator\n    except ZeroDivisionError:\n        return 0<\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u5c06\u4e24\u4e2a\u6574\u6570\u76f8\u9664\uff0c\u5e76\u5728\u5206\u6bcd\u4e3a\u96f6\u65f6\u8fd4\u56de\u4e00\u4e2a <code>ZeroDivisionError<\/code> \u5f02\u5e38\u3002\u5982\u679c\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u9891\u7e41\u8c03\u7528\u6b64\u51fd\u6570\uff1a<\/p>\n<pre>for i in range(1000000):\n    divide(i, i % 10)<\/pre>\n<p>\u5728\u8d4b\u503c\u8bed\u53e5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6a21 10 \u64cd\u4f5c\u6765\u5f15\u5165\u9891\u7e41\u7684 <code>ZeroDivisionError<\/code> \u5f02\u5e38\u3002\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u7684\u5f00\u9500\u5c06\u663e\u7740\u5f71\u54cd\u7a0b\u5e8f\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u662f\u4e00\u79cd\u5904\u7406\u9519\u8bef\u7684\u6709\u6548\u65b9\u5f0f\uff0c\u4f46\u662f\u5bf9\u4e8e\u6027\u80fd\u654f\u611f\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u9700\u8981\u8003\u8651\u5176\u5f00\u9500\u3002\u901a\u8fc7\u4e86\u89e3\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f71\u54cd\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u505a\u51fa\u660e\u667a\u7684\u51b3\u7b56\uff0c\u9009\u62e9\u6700\u9002\u5408\u5176\u7279\u5b9a\u5e94\u7528\u7a0b\u5e8f\u7684\u5f02\u5e38\u5904\u7406\u7b56\u7565\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f71\u54cd\u662f\u4ec0\u4e48\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f71\u54cd\u662f&#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-203096","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203096","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=203096"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203096\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}