{"id":2478,"date":"2017-07-20T10:55:57","date_gmt":"2017-07-20T02:55:57","guid":{"rendered":"http:\/\/cn.hostease.com\/xueyuan\/?p=2478"},"modified":"2025-01-08T16:17:13","modified_gmt":"2025-01-08T08:17:13","slug":"python%e8%bf%9b%e9%98%b608-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/xueyuan\/jishu\/python\/python%e8%bf%9b%e9%98%b608-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86\/","title":{"rendered":"Python\u8fdb\u963608 \u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<h3>\u5f02\u5e38\u5904\u7406<\/h3>\n<p>\u5728\u9879\u76ee\u5f00\u53d1\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002\u5f02\u5e38\u5904\u7406\u5e2e\u52a9\u4eba\u4eecdebug\uff0c\u901a\u8fc7\u66f4\u52a0\u4e30\u5bcc\u7684\u4fe1\u606f\uff0c\u8ba9\u4eba\u4eec\u66f4\u5bb9\u6613\u627e\u5230bug\u7684\u6240\u5728\u3002\u5f02\u5e38\u5904\u7406\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5bb9\u9519\u6027\u3002<\/p>\n<p>\u6211\u4eec\u4e4b\u524d\u5728\u8bb2\u5faa\u73af\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u66fe\u63d0\u5230\u4e00\u4e2aStopIteration\u7684\u5f02\u5e38\uff0c\u8be5\u5f02\u5e38\u662f\u5728\u5faa\u73af\u5bf9\u8c61\u7a77\u5c3d\u6240\u6709\u5143\u7d20\u65f6\u7684\u62a5\u9519\u3002<\/p>\n<p>\u6211\u4eec\u4ee5\u5b83\u4e3a\u4f8b\uff0c\u6765\u8bf4\u660e\u57fa\u672c\u7684\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<p>\u4e00\u4e2a\u5305\u542b\u5f02\u5e38\u7684\u7a0b\u5e8f:<\/p>\n<div class=\"cnblogs_code\">\n<pre>re = iter(range(5))\n\nfor i in range(100):\n    print re.next()\n\nprint 'HaHaHaHa'<\/pre>\n<\/div>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5faa\u73af\u5bf9\u8c61re\uff0c\u8be5\u5faa\u73af\u5bf9\u8c61\u5c06\u8fdb\u884c5\u6b21\u5faa\u73af\uff0c\u6bcf\u6b21\u4f7f\u7528\u5e8f\u5217\u7684\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p>\u5728\u968f\u540e\u7684for\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u624b\u5de5\u8c03\u7528next()\u51fd\u6570\u3002\u5f53\u5faa\u73af\u8fdb\u884c\u5230\u7b2c6\u6b21\u7684\u65f6\u5019\uff0cre.next()\u4e0d\u4f1a\u518d\u8fd4\u56de\u5143\u7d20\uff0c\u800c\u662f\u629b\u51fa(raise)StopIteration\u7684\u5f02\u5e38\u3002\u6574\u4e2a\u7a0b\u5e8f\u5c06\u4f1a\u4e2d\u65ad\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u4ee5\u4e0a\u5f02\u5e38\u7a0b\u5e8f\uff0c\u76f4\u5230\u5b8c\u7f8e\u7684\u6ca1\u6709bug\u3002\u4f46\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u6211\u4eec\u5728\u5199\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u77e5\u9053\u8fd9\u91cc\u53ef\u80fd\u72af\u9519\u4ee5\u53ca\u53ef\u80fd\u7684\u72af\u9519\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u9488\u5bf9\u8be5\u5f02\u5e38\u7c7b\u578b\u5b9a\u4e49\u597d\u201d\u5e94\u6025\u9884\u6848\u201c\u3002<\/p>\n<div class=\"cnblogs_code\">\n<pre>re = iter(range(5))\n\ntry:\n    for i in range(100):\n        print re.next()\nexcept StopIteration:\n    print 'here is end ',i\n\nprint 'HaHaHaHa'<\/pre>\n<\/div>\n<p>\u5728try\u7a0b\u5e8f\u6bb5\u4e2d\uff0c\u6211\u4eec\u653e\u5165\u5bb9\u6613\u72af\u9519\u7684\u90e8\u5206\u3002\u6211\u4eec\u53ef\u4ee5\u8ddf\u4e0aexcept\uff0c\u6765\u8bf4\u660e\u5982\u679c\u5728try\u90e8\u5206\u7684\u8bed\u53e5\u53d1\u751fStopIteration\u65f6\uff0c\u7a0b\u5e8f\u8be5\u505a\u7684\u4e8b\u60c5\u3002\u5982\u679c\u6ca1\u6709\u53d1\u751f\u5f02\u5e38\uff0c\u5219except\u90e8\u5206\u88ab\u8df3\u8fc7\u3002<\/p>\n<p>\u968f\u540e\uff0c\u7a0b\u5e8f\u5c06\u7ee7\u7eed\u8fd0\u884c\uff0c\u800c\u4e0d\u662f\u5f7b\u5e95\u4e2d\u65ad\u3002<\/p>\n<p>\u5b8c\u6574\u7684\u8bed\u6cd5\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>try:\n    ...\nexcept exception1:\n    ...\nexcept exception2:\n    ...\nexcept:\n    ...\nelse:\n    ...\nfinally:\n    ...<\/pre>\n<\/div>\n<p>\u5982\u679ctry\u4e2d\u6709\u5f02\u5e38\u53d1\u751f\u65f6\uff0c\u5c06\u6267\u884c\u5f02\u5e38\u7684\u5f52\u5c5e\uff0c\u6267\u884cexcept\u3002\u5f02\u5e38\u5c42\u5c42\u6bd4\u8f83\uff0c\u770b\u662f\u5426\u662fexception1, exception2&#8230;\uff0c\u76f4\u5230\u627e\u5230\u5176\u5f52\u5c5e\uff0c\u6267\u884c\u76f8\u5e94\u7684except\u4e2d\u7684\u8bed\u53e5\u3002\u5982\u679cexcept\u540e\u9762\u6ca1\u6709\u4efb\u4f55\u53c2\u6570\uff0c\u90a3\u4e48\u8868\u793a\u6240\u6709\u7684exception\u90fd\u4ea4\u7ed9\u8fd9\u6bb5\u7a0b\u5e8f\u5904\u7406\u3002\u6bd4\u5982:<\/p>\n<div class=\"cnblogs_code\">\n<pre>try:\n    print(a*2)\nexcept TypeError:\n    print(\"TypeError\")\nexcept:\n    print(\"Not Type Error &amp; Error noted\")<\/pre>\n<\/div>\n<p>\u7531\u4e8ea\u6ca1\u6709\u5b9a\u4e49\uff0c\u6240\u4ee5\u662fNameError\u3002\u5f02\u5e38\u6700\u7ec8\u88abexcept:\u90e8\u5206\u7684\u7a0b\u5e8f\u6355\u6349\u3002<\/p>\n<p>\u5982\u679c\u65e0\u6cd5\u5c06\u5f02\u5e38\u4ea4\u7ed9\u5408\u9002\u7684\u5bf9\u8c61\uff0c\u5f02\u5e38\u5c06\u7ee7\u7eed\u5411\u4e0a\u5c42\u629b\u51fa\uff0c\u76f4\u5230\u88ab\u6355\u6349\u6216\u8005\u9020\u6210\u4e3b\u7a0b\u5e8f\u62a5\u9519\u3002\u6bd4\u5982\u4e0b\u9762\u7684\u7a0b\u5e8f<\/p>\n<div class=\"cnblogs_code\">\n<pre>def test_func():\n    try:\n        m = 1\/0\n    except NameError:\n        print(\"Catch NameError in the sub-function\")\n\ntry:\n    test_func()\nexcept ZeroDivisionError:\n    print(\"Catch error in the main program\")<\/pre>\n<\/div>\n<p>\u5b50\u7a0b\u5e8f\u7684try&#8230;except&#8230;\u7ed3\u6784\u65e0\u6cd5\u5904\u7406\u76f8\u5e94\u7684\u9664\u4ee50\u7684\u9519\u8bef\uff0c\u6240\u4ee5\u9519\u8bef\u88ab\u629b\u7ed9\u4e0a\u5c42\u7684\u4e3b\u7a0b\u5e8f\u3002<\/p>\n<p>\u5982\u679ctry\u4e2d\u6ca1\u6709\u5f02\u5e38\uff0c\u90a3\u4e48except\u90e8\u5206\u5c06\u8df3\u8fc7\uff0c\u6267\u884celse\u4e2d\u7684\u8bed\u53e5\u3002<\/p>\n<p>finally\u662f\u65e0\u8bba\u662f\u5426\u6709\u5f02\u5e38\uff0c\u6700\u540e\u90fd\u8981\u505a\u7684\u4e00\u4e9b\u4e8b\u60c5\u3002<\/p>\n<p>\u6d41\u7a0b\u5982\u4e0b\uff0c<\/p>\n<p>try-&gt;\u5f02\u5e38-&gt;except-&gt;finally<\/p>\n<p>try-&gt;\u65e0\u5f02\u5e38-&gt;else-&gt;finally<\/p>\n<h3>\u629b\u51fa\u5f02\u5e38<\/h3>\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5199\u4e00\u4e2a\u629b\u51fa\u5f02\u5e38\u7684\u4f8b\u5b50:<\/p>\n<div class=\"cnblogs_code\">\n<pre>print 'Lalala'\nraise StopIteration\nprint 'Hahaha'<\/pre>\n<\/div>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e0d\u5177\u5907\u4efb\u4f55\u5b9e\u9645\u610f\u4e49\u3002\u53ea\u662f\u4e3a\u4e86\u8bf4\u660eraise\u8bed\u53e5\u7684\u4f5c\u7528\u3002<\/p>\n<p>StopIteration\u662f\u4e00\u4e2a\u7c7b\u3002\u629b\u51fa\u5f02\u5e38\u65f6\uff0c\u4f1a\u81ea\u52a8\u6709\u4e00\u4e2a\u4e2d\u95f4\u73af\u8282\uff0c\u5c31\u662f\u751f\u6210StopIteration\u7684\u4e00\u4e2a\u5bf9\u8c61\u3002Python\u5b9e\u9645\u4e0a\u629b\u51fa\u7684\uff0c\u662f\u8fd9\u4e2a\u5bf9\u8c61\u3002\u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u81ea\u884c\u751f\u6210\u5bf9\u8c61:<\/p>\n<div class=\"cnblogs_code\">\n<pre>raise StopIteration()<\/pre>\n<\/div>\n<h3>\u603b\u7ed3<\/h3>\n<p>try: &#8230; except exception: &#8230; else: &#8230; finally: &#8230;<br \/>\nraise exception<\/p>\n<p>\u4f5c\u8005\uff1aVamei \u51fa\u5904\uff1ahttps:\/\/www.cnblogs.com\/vamei<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u5e38\u5904\u7406 \u5728\u9879\u76ee\u5f00\u53d1\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002\u5f02\u5e38\u5904\u7406\u5e2e\u52a9\u4eba\u4eecdebug\uff0c\u901a\u8fc7\u66f4\u52a0\u4e30\u5bcc\u7684\u4fe1\u606f\uff0c\u8ba9\u4eba\u4eec\u66f4\u5bb9\u6613\u627e\u5230 &#8230; <a title=\"Python\u8fdb\u963608 \u5f02\u5e38\u5904\u7406\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/xueyuan\/jishu\/python\/python%e8%bf%9b%e9%98%b608-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86\/\" aria-label=\"\u9605\u8bfb Python\u8fdb\u963608 \u5f02\u5e38\u5904\u7406\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[654],"tags":[729],"class_list":["post-2478","post","type-post","status-publish","format-standard","hentry","category-python","tag-python-"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/comments?post=2478"}],"version-history":[{"count":2,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2478\/revisions"}],"predecessor-version":[{"id":9205,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2478\/revisions\/9205"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/media?parent=2478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/categories?post=2478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/tags?post=2478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}