{"id":202842,"date":"2025-05-22T08:51:45","date_gmt":"2025-05-22T00:51:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202842\/"},"modified":"2025-05-22T08:51:45","modified_gmt":"2025-05-22T00:51:45","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%bc%82%e5%b8%b8%e6%9d%a5%e9%98%b2%e6%ad%a2%e4%b8%8d%e6%ad%a3%e7%a1%ae%e7%9a%84%e8%be%93%e5%85%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202842\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u6765\u9632\u6b62\u4e0d\u6b63\u786e\u7684\u8f93\u5165\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u6765\u9632\u6b62\u4e0d\u6b63\u786e\u7684\u8f93\u5165\uff1f<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u6765\u9632\u6b62\u4e0d\u6b63\u786e\u7684\u8f93\u5165\uff1f\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6280\u672f\u53ef\u4ee5\u9632\u6b62\u4e0d\u6b63\u786e\u7684\u8f93\u5165\u3002\u5f02\u5e38\u5904\u7406\u4f7f\u7528 try \u548c except \u8bed\u53e5\uff0ctry \u5757\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0cexcept \u5757\u5305\u542b\u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801\u3002\u901a\u8fc7\u4f7f\u7528 ValueError \u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u4e0d\u6b63\u786e\u7684\u6570\u503c\u6216\u5176\u4ed6\u7c7b\u578b\u7684\u503c\uff0c\u4ece\u800c\u907f\u514d\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83\u3002Python \u4e2d\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u5982 TypeError\u3001IndexError \u548c KeyError\uff0c\u7528\u4e8e\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u9519\u8bef\u3002\u901a\u8fc7\u5904\u7406\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u66f4\u5065\u58ee\u3001\u66f4\u6613\u4e8e\u7ef4\u62a4\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729949492671cef342f69e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u6765\u9632\u6b62\u4e0d\u6b63\u786e\u7684\u8f93\u5165<\/strong><\/p>\n<p>\u5728\u7f16\u7a0b\u4e2d\uff0c\u9519\u8bef\u7684\u8f93\u5165\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\uff0c\u751a\u81f3\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83\u3002\u4e3a\u4e86\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6280\u672f\u6765\u6355\u83b7\u548c\u5904\u7406\u4e0d\u6b63\u786e\u7684\u8f93\u5165\uff0c\u4ece\u800c\u907f\u514d\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5728 Python \u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u4f7f\u7528 <code>try<\/code> \u548c <code>except<\/code> \u8bed\u53e5\u3002<code>try<\/code> \u5757\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u800c <code>except<\/code> \u5757\u5305\u542b\u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>\u5904\u7406\u4e0d\u6b63\u786e\u7684\u8f93\u5165<\/strong><\/p>\n<p>\u8981\u5904\u7406\u4e0d\u6b63\u786e\u7684\u8f93\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>ValueError<\/code> \u5f02\u5e38\u3002<code>ValueError<\/code> \u5f02\u5e38\u7528\u4e8e\u8868\u793a\u4e0d\u6b63\u786e\u7684\u6570\u503c\u6216\u5176\u4ed6\u7c7b\u578b\u7684\u503c\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u6765\u9632\u6b62\u4e0d\u6b63\u786e\u7684\u8f93\u5165\uff1a<\/p>\n<pre># \u5c1d\u8bd5\u83b7\u53d6\u7528\u6237\u8f93\u5165\ntry:\n    number = int(input(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff1a\"))\n\n# \u5982\u679c\u8f93\u5165\u4e0d\u662f\u6574\u6570\uff0c\u5219\u5f15\u53d1 ValueError \u5f02\u5e38\nexcept ValueError:\n    print(\"\u8f93\u5165\u7684\u4e0d\u662f\u6574\u6570\")\n\n# \u5904\u7406\u6b63\u786e\u7684\u8f93\u5165\nelse:\n    print(\"\u8f93\u5165\u7684\u6570\u5b57\u4e3a\uff1a\", number)<\/pre>\n<p><strong>\u6267\u884c\u6b64\u4ee3\u7801<\/strong><\/p>\n<p>\u8fd0\u884c\u6b64\u4ee3\u7801\u540e\uff0c\u5b83\u5c06\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\uff0c\u5219\u7a0b\u5e8f\u5c06\u6253\u5370\u51fa\u8f93\u5165\u7684\u6570\u5b57\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u975e\u6574\u6570\uff0c\u5219\u7a0b\u5e8f\u5c06\u6253\u5370\u4e00\u6761\u9519\u8bef\u6d88\u606f\uff0c\u8868\u793a\u8f93\u5165\u7684\u4e0d\u662f\u6574\u6570\u3002<\/p>\n<p><strong>\u5176\u4ed6\u5f02\u5e38<\/strong><\/p>\n<p>\u9664\u4e86 <code>ValueError<\/code> \u4e4b\u5916\uff0cPython \u4e2d\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5f02\u5e38\u53ca\u5176\u7528\u9014\uff1a<\/p>\n<ul>\n<li><code>TypeError<\/code>\uff1a\u6307\u793a\u53c2\u6570\u7c7b\u578b\u4e0d\u6b63\u786e<\/li>\n<li><code>IndexError<\/code>\uff1a\u6307\u793a\u7d22\u5f15\u8d85\u51fa\u5217\u8868\u6216\u5143\u7ec4\u8303\u56f4<\/li>\n<li><code>KeyError<\/code>\uff1a\u6307\u793a\u5b57\u5178\u4e2d\u6ca1\u6709\u627e\u5230\u952e<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u5904\u7406\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u66f4\u5065\u58ee\u3001\u66f4\u6613\u4e8e\u7ef4\u62a4\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u6765\u9632\u6b62\u4e0d\u6b63\u786e\u7684\u8f93\u5165\uff1f&#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-202842","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202842","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=202842"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202842\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}