{"id":7043,"date":"2022-09-01T21:21:23","date_gmt":"2022-09-01T13:21:23","guid":{"rendered":"http:\/\/139.9.1.231\/?p=7043"},"modified":"2022-09-01T21:21:25","modified_gmt":"2022-09-01T13:21:25","slug":"try-except-assert","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/09\/01\/try-except-assert\/","title":{"rendered":"python \u5f02\u5e38\u5904\u7406 try except \u548c \u65ad\u8a00(assert)"},"content":{"rendered":"\n\n\n<p>\u6700\u8fd1\u5728\u5199\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u5f88\u591a\u65f6\u5019\u9700\u8981\u8003\u8651\u5404\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u4ec5\u4ec5\u4f7f\u7528if\uff0c\u4f1a\u5f88\u9ebb\u70e6\uff0c\u4e8e\u662f\u60f3\u5230\u4e86python \u5f02\u5e38\u5904\u7406\u548c\u65ad\u8a00\uff0c\u7528\u4e8e\u5224\u65ad\u51fd\u6570\u8fdb\u7a0b\u3002\u3001<\/p>\n\n\n\n<h2><a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/zh-cn\/3\/tutorial\/errors.html\" target=\"_blank\">\u5f02\u5e38\u5904\u7406 try<\/a><\/h2>\n\n\n\n<p>\u7a0b\u5e8f\u5728\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u5982\u679cpython\u89e3\u91ca\u5668\u9047\u5230\u4e00\u4e2a\u9519\u8bef\uff0c\u4f1a\u505c\u6b62\u7a0b\u5e8f\u7684\u6267\u884c\uff0c<br>\u5e76\u4e14\u63d0\u793a\u4e00\u4e9b\u9519\u8bef\u7684\u4fe1\u606f\uff0c\u8fd9\u5c31\u662f\u5f02\u5e38<br>\u6211\u4eec\u5728\u7a0b\u5e8f\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u5f88\u96be\u5c06\u6240\u6709\u7684\u7279\u6b8a\u60c5\u51b5\u90fd\u5904\u7406\uff0c<br>\u901a\u8fc7\u5f02\u5e38\u6355\u83b7\u53ef\u4ee5\u9488\u5bf9<br>\u7a81\u53d1\u4e8b\u4ef6\u505a\u96c6\u4e2d\u5904\u7406\uff0c\u4ece\u800c\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7a33\u5b9a\u6027<\/p>\n\n\n\n<p>\u5728\u7a0b\u5e8f\u5f00\u53d1\u4e2d\uff0c\u5982\u679c\u5bf9\u67d0\u4e9b\u4ee3\u7801\u7684\u6267\u884c\u4e0d\u80fd\u786e\u5b9a(\u7a0b\u5e8f\u8bed\u6cd5\u5b8c\u5168\u6b63\u786e)<br>\u53ef\u4ee5\u589e\u52a0try\u6765\u6355\u83b7\u5f02\u5e38<\/p>\n\n\n\n<p class=\"has-bright-blue-background-color has-background\">try\u8fd9\u4e2a\u5173\u952e\u5b57\u6765\u6355\u83b7\u5f02\u5e38<br>try:\u5c1d\u8bd5\u6267\u884c\u7684\u4ee3\u7801<br>except:\u51fa\u73b0\u9519\u8bef\u7684\u5904\u7406                                                                                                                    finally\uff1a\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884cfinal\u90e8\u4efd<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>try<\/strong>:\n    print('try...')\n    r = 10 \/ int('a')\n    print('result:', r)\n<strong>except<\/strong> ValueError <strong>as<\/strong> e:\n    print('ValueError:', e)\n<strong>except<\/strong> ZeroDivisionError <strong>as<\/strong> e:\n    print('ZeroDivisionError:', e)\n<strong>finally<\/strong>:\n    print('finally...')\nprint('END')<\/code><\/pre>\n\n\n\n<p><code>try<\/code>\u00a0\u8bed\u53e5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul><li>\u9996\u5148\uff0c\u6267\u884c\u00a0<em>try \u5b50\u53e5<\/em>\u00a0\uff08<code>try<\/code>\u00a0\u548c\u00a0<code>except<\/code>\u00a0\u5173\u952e\u5b57\u4e4b\u95f4\u7684\uff08\u591a\u884c\uff09\u8bed\u53e5\uff09\u3002<\/li><li>\u5982\u679c\u6ca1\u6709\u89e6\u53d1\u5f02\u5e38\uff0c\u5219\u8df3\u8fc7\u00a0<em>except \u5b50\u53e5<\/em>\uff0c<code>try<\/code>\u00a0\u8bed\u53e5\u6267\u884c\u5b8c\u6bd5\u3002<\/li><li>\u5982\u679c\u5728\u6267\u884c\u00a0<code>try<\/code>\u00a0\u5b50\u53e5\u65f6\u53d1\u751f\u4e86\u5f02\u5e38\uff0c\u5219\u8df3\u8fc7\u8be5\u5b50\u53e5\u4e2d\u5269\u4e0b\u7684\u90e8\u5206\u3002 \u5982\u679c\u5f02\u5e38\u7684\u7c7b\u578b\u4e0e\u00a0<code>except<\/code>\u00a0\u5173\u952e\u5b57\u540e\u6307\u5b9a\u7684\u5f02\u5e38\u76f8\u5339\u914d\uff0c\u5219\u4f1a\u6267\u884c\u00a0<em>except \u5b50\u53e5<\/em>\uff0c\u7136\u540e\u8df3\u5230 try\/except \u4ee3\u7801\u5757\u4e4b\u540e\u7ee7\u7eed\u6267\u884c\u3002<\/li><li>\u5982\u679c\u53d1\u751f\u7684\u5f02\u5e38\u4e0e\u00a0<em>except \u5b50\u53e5<\/em>\u00a0\u4e2d\u6307\u5b9a\u7684\u5f02\u5e38\u4e0d\u5339\u914d\uff0c\u5219\u5b83\u4f1a\u88ab\u4f20\u9012\u5230\u5916\u90e8\u7684\u00a0<code>try<\/code>\u00a0\u8bed\u53e5\u4e2d\uff1b\u5982\u679c\u6ca1\u6709\u627e\u5230\u5904\u7406\u7a0b\u5e8f\uff0c\u5219\u5b83\u662f\u4e00\u4e2a\u00a0<em>\u672a\u5904\u7406\u5f02\u5e38<\/em>\u00a0\u4e14\u6267\u884c\u5c06\u7ec8\u6b62\u5e76\u8f93\u51fa\u5982\u4e0a\u6240\u793a\u7684\u6d88\u606f\u3002<\/li><\/ul>\n\n\n\n<p><code>try<\/code>\u00a0\u8bed\u53e5\u53ef\u4ee5\u6709\u591a\u4e2a\u00a0<em>except \u5b50\u53e5<\/em>\u00a0\u6765\u4e3a\u4e0d\u540c\u7684\u5f02\u5e38\u6307\u5b9a\u5904\u7406\u7a0b\u5e8f\u3002 \u4f46\u6700\u591a\u53ea\u6709\u4e00\u4e2a\u5904\u7406\u7a0b\u5e8f\u4f1a\u88ab\u6267\u884c\u3002 \u5904\u7406\u7a0b\u5e8f\u53ea\u5904\u7406\u5bf9\u5e94\u7684\u00a0<em>try \u5b50\u53e5<\/em>\u00a0\u4e2d\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u800c\u4e0d\u5904\u7406\u540c\u4e00\u00a0<code>try<\/code>\u00a0\u8bed\u53e5\u5185\u5176\u4ed6\u5904\u7406\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\u3002\u00a0<em>except \u5b50\u53e5<\/em>\u00a0\u53ef\u4ee5\u7528\u5e26\u5706\u62ec\u53f7\u7684\u5143\u7ec4\u6765\u6307\u5b9a\u591a\u4e2a\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u5e38\u89c1\u5f02\u5e38\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" width=\"565\" height=\"945\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/09\/image-16.png\" alt=\"\" class=\"wp-image-7172\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/09\/image-16.png 565w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/09\/image-16-179x300.png 179w\" sizes=\"(max-width: 565px) 100vw, 565px\" \/><\/figure><\/div>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><code>try<\/code>\u00a0\u8bed\u53e5\u8fd8\u6709\u4e00\u4e2a\u53ef\u9009\u5b50\u53e5\uff0c\u7528\u4e8e\u5b9a\u4e49\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u5fc5\u987b\u8981\u6267\u884c\u7684\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5b58\u5728\u00a0<code>finally<\/code>\u00a0\u5b50\u53e5\uff0c\u5219\u00a0<code>finally<\/code>\u00a0\u5b50\u53e5\u662f\u00a0<code>try<\/code>\u00a0\u8bed\u53e5\u7ed3\u675f\u524d\u6267\u884c\u7684\u6700\u540e\u4e00\u9879\u4efb\u52a1\u3002\u4e0d\u8bba\u00a0<code>try<\/code>\u00a0\u8bed\u53e5\u662f\u5426\u89e6\u53d1\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884c\u00a0<code>finally<\/code>\u00a0\u5b50\u53e5\u3002\u4ee5\u4e0b\u5185\u5bb9\u4ecb\u7ecd\u4e86\u51e0\u79cd\u6bd4\u8f83\u590d\u6742\u7684\u89e6\u53d1\u5f02\u5e38\u60c5\u666f\uff1a<\/p>\n\n\n\n<ul><li>\u5982\u679c\u6267\u884c\u00a0<code>try<\/code>\u00a0\u5b50\u53e5\u671f\u95f4\u89e6\u53d1\u4e86\u67d0\u4e2a\u5f02\u5e38\uff0c\u5219\u67d0\u4e2a\u00a0<code>except<\/code>\u00a0\u5b50\u53e5\u5e94\u5904\u7406\u8be5\u5f02\u5e38\u3002\u5982\u679c\u8be5\u5f02\u5e38\u6ca1\u6709\u00a0<code>except<\/code>\u00a0\u5b50\u53e5\u5904\u7406\uff0c\u5728\u00a0<code>finally<\/code>\u00a0\u5b50\u53e5\u6267\u884c\u540e\u4f1a\u88ab\u91cd\u65b0\u89e6\u53d1\u3002<\/li><li><code>except<\/code>\u00a0\u6216\u00a0<code>else<\/code>\u00a0\u5b50\u53e5\u6267\u884c\u671f\u95f4\u4e5f\u4f1a\u89e6\u53d1\u5f02\u5e38\u3002 \u540c\u6837\uff0c\u8be5\u5f02\u5e38\u4f1a\u5728\u00a0<code>finally<\/code>\u00a0\u5b50\u53e5\u6267\u884c\u4e4b\u540e\u88ab\u91cd\u65b0\u89e6\u53d1\u3002<\/li><li>\u5982\u679c\u00a0<code>finally<\/code>\u00a0\u5b50\u53e5\u4e2d\u5305\u542b\u00a0<code>break<\/code>\u3001<code>continue<\/code>\u00a0\u6216\u00a0<code>return<\/code>\u00a0\u7b49\u8bed\u53e5\uff0c\u5f02\u5e38\u5c06\u4e0d\u4f1a\u88ab\u91cd\u65b0\u5f15\u53d1\u3002<\/li><li>\u5982\u679c\u6267\u884c\u00a0<code>try<\/code>\u00a0\u8bed\u53e5\u65f6\u9047\u5230\u00a0<code>break<\/code>,\u3001<code>continue<\/code>\u00a0\u6216\u00a0<code>return<\/code>\u00a0\u8bed\u53e5\uff0c\u5219\u00a0<code>finally<\/code>\u00a0\u5b50\u53e5\u5728\u6267\u884c\u00a0<code>break<\/code>\u3001<code>continue<\/code>\u00a0\u6216\u00a0<code>return<\/code>\u00a0\u8bed\u53e5\u4e4b\u524d\u6267\u884c\u3002<\/li><li>\u5982\u679c\u00a0<code>finally<\/code>\u00a0\u5b50\u53e5\u4e2d\u5305\u542b\u00a0<code>return<\/code>\u00a0\u8bed\u53e5\uff0c\u5219\u8fd4\u56de\u503c\u6765\u81ea\u00a0<code>finally<\/code>\u00a0\u5b50\u53e5\u7684\u67d0\u4e2a\u00a0<code>return<\/code>\u00a0\u8bed\u53e5\u7684\u8fd4\u56de\u503c\uff0c\u800c\u4e0d\u662f\u6765\u81ea\u00a0<code>try<\/code>\u00a0\u5b50\u53e5\u7684\u00a0<code>return<\/code>\u00a0\u8bed\u53e5\u7684\u8fd4\u56de\u503c\u3002<\/li><\/ul>\n\n\n\n<h2> assert\uff08\u65ad\u8a00\uff09 <\/h2>\n\n\n\n<p>Python assert\uff08\u65ad\u8a00\uff09\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u5728\u8868\u8fbe\u5f0f\u6761\u4ef6\u4e3a false \u7684\u65f6\u5019\u89e6\u53d1\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u65ad\u8a00\u53ef\u4ee5\u5728\u6761\u4ef6\u4e0d\u6ee1\u8db3\u7a0b\u5e8f\u8fd0\u884c\u7684\u60c5\u51b5\u4e0b\u76f4\u63a5\u8fd4\u56de\u9519\u8bef\uff0c\u800c\u4e0d\u5fc5\u7b49\u5f85\u7a0b\u5e8f\u8fd0\u884c\u540e\u51fa\u73b0\u5d29\u6e83\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u6211\u4eec\u7684\u4ee3\u7801\u53ea\u80fd\u5728 Linux \u7cfb\u7edf\u4e0b\u8fd0\u884c\uff0c\u53ef\u4ee5\u5148\u5224\u65ad\u5f53\u524d\u7cfb\u7edf\u662f\u5426\u7b26\u5408\u6761\u4ef6\u3002<\/p>\n\n\n\n<p>\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">assert expression<\/pre>\n\n\n\n<p>\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if not expression:\n    raise AssertionError<\/pre>\n\n\n\n<p>assert \u540e\u9762\u4e5f\u53ef\u4ee5\u7d27\u8ddf\u53c2\u6570:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">assert expression [, arguments]<\/pre>\n\n\n\n<p>\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if not expression:\n    raise AssertionError(arguments)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u5199\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u5f88\u591a\u65f6\u5019\u9700\u8981\u8003\u8651\u5404\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u4ec5\u4ec5\u4f7f\u7528if\uff0c\u4f1a\u5f88\u9ebb\u70e6\uff0c\u4e8e\u662f\u60f3\u5230\u4e86python \u5f02\u5e38\u5904\u7406\u548c\u65ad &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/09\/01\/try-except-assert\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">python \u5f02\u5e38\u5904\u7406 try except \u548c \u65ad\u8a00(assert)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/7043"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=7043"}],"version-history":[{"count":19,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/7043\/revisions"}],"predecessor-version":[{"id":7177,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/7043\/revisions\/7177"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=7043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=7043"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=7043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}