{"id":271,"date":"2023-10-27T09:54:11","date_gmt":"2023-10-27T01:54:11","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=271"},"modified":"2023-10-31T16:23:16","modified_gmt":"2023-10-31T08:23:16","slug":"%e3%80%8apython%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%e3%80%8b8-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/27\/%e3%80%8apython%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%e3%80%8b8-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8\/","title":{"rendered":"\u300aPython\u5165\u95e8\u6559\u7a0b\u300b8. \u9519\u8bef\u548c\u5f02\u5e38"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5f00\u80c3\u83dc<\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/2-%e4%bd%bf%e7%94%a8-python-%e8%a7%a3%e9%87%8a%e5%99%a8\/\">\u4f7f\u7528 Python \u89e3\u91ca\u5668<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/3-%e9%9d%9e%e6%ad%a3%e5%bc%8f%e4%bb%8b%e7%bb%8dpython\/\">\u975e\u6b63\u5f0f\u4ecb\u7ecdPython<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/4-%e6%b7%b1%e5%85%a5%e6%b5%81%e7%a8%8b%e6%8e%a7%e5%88%b6\/\">\u6df1\u5165\u6d41\u7a0b\u63a7\u5236<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/5-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/\">\u6570\u636e\u7ed3\u6784<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/6-%e6%a8%a1%e5%9d%97\/\">\u6a21\u5757<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/7-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba\/\">\u8f93\u5165\u548c\u8f93\u51fa<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/8-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8\/\">\u9519\u8bef\u548c\u5f02\u5e38<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/9-%e7%b1%bb\/\">\u7c7b<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/10-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/11-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d%ef%bc%88%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%89\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd\uff08\u7b2c\u4e8c\u90e8\u5206\uff09<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/12-%e7%8e%b0%e5%9c%a8%e5%b9%b2%e4%bb%80%e4%b9%88\/\">\u73b0\u5728\u5e72\u4ec0\u4e48?<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/13-%e4%ba%a4%e4%ba%92%e5%bc%8f%e8%be%93%e5%85%a5%e7%bc%96%e8%be%91%e5%8f%8a%e5%8e%86%e5%8f%b2%e6%9b%bf%e4%bb%a3\/\">\u4ea4\u4e92\u5f0f\u8f93\u5165\u7f16\u8f91\u53ca\u5386\u53f2\u66ff\u4ee3<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/14-%e6%b5%ae%e7%82%b9%e7%ae%97%e6%9c%af-%e9%97%ae%e9%a2%98%e5%92%8c%e9%99%90%e5%88%b6\/\">\u6d6e\u70b9\u7b97\u672f: \u95ee\u9898\u548c\u9650\u5236<\/a><\/li>\n<\/ol>\n\n\n\n<p>\u5230\u73b0\u5728\u4e3a\u6b62, \u6ca1\u6709\u66f4\u591a\u7684\u63d0\u53ca\u9519\u8bef\u4fe1\u606f, \u4f46\u662f\u5f53\u4f60\u5728\u5c1d\u8bd5\u8fd9\u4e9b\u4f8b\u5b50\u65f6, \u6216\u591a\u6216\u5c11\u4f1a\u78b0\u5230\u4e00\u4e9b. \u8fd9\u91cc (\u81f3\u5c11) \u6709\u4e24\u79cd\u53ef\u4ee5\u5206\u8fa8\u7684\u9519\u8bef:&nbsp;<em>syntax error<\/em>&nbsp;\u548c&nbsp;<em>exception<\/em>&nbsp;, \u6309\u4e2d\u6587\u6765\u8bf4, \u5c31\u662f\u8bed\u6cd5\u9519\u8bef\u548c\u5f02\u5e38.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8.1. \u8bed\u6cd5\u9519\u8bef<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/errors.html#tut-syntaxerrors\"><\/a><\/h2>\n\n\n\n<p>\u8bed\u6cd5\u9519\u8bef, \u4e5f\u53ef\u4ee5\u8ba4\u4e3a\u662f\u89e3\u6790\u65f6\u9519\u8bef, \u8fd9\u662f\u5728\u4f60\u5b66\u4e60 Python \u8fc7\u7a0b\u4e2d\u6700\u6709\u53ef\u80fd\u78b0\u5230\u7684:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0whileTrue<\/strong>\u00a0print(\u2018Hello world\u2019)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> File \u201c&lt;stdin>\u201d, line 1, in ? <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">while True print(\u2018Hello world\u2019) ^ <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">SyntaxError: invalid syntax<\/p>\n\n\n\n<p>\u89e3\u6790\u5668\u4f1a\u91cd\u590d\u51fa\u9519\u7684\u90a3\u884c, \u7136\u540e\u663e\u793a\u4e00\u4e2a\u5c0f\u7bad\u5934, \u6307\u51fa\u63a2\u6d4b\u5230\u9519\u8bef\u65f6\u6700\u65e9\u7684\u90a3\u4e2a\u70b9. \u9519\u8bef\u4e00\u822c\u662f\u7531\u7bad\u5934\u6240\u6307\u7684\u5730\u65b9\u5bfc\u81f4 (\u6216\u8005\u81f3\u5c11\u662f\u6b64\u5904\u88ab\u63a2\u6d4b\u5230): \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d, \u9519\u8bef\u662f\u5728&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#print\"><code>print()<\/code><\/a>&nbsp;\u51fd\u6570\u8fd9\u91cc\u88ab\u53d1\u73b0\u7684, \u56e0\u4e3a\u5728\u5b83\u4e4b\u524d\u5c11\u4e86\u4e00\u4e2a\u5192\u53f7 (<code>':'<\/code>). \u6587\u4ef6\u7684\u540d\u79f0\u4e0e\u884c\u53f7\u4f1a\u88ab\u6253\u5370\u51fa\u6765, \u4ee5\u4fbf\u4e8e\u4f60\u80fd\u627e\u5230\u4e00\u4e2a\u811a\u672c\u4e2d\u5bfc\u81f4\u9519\u8bef\u7684\u5730\u65b9.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8.2. \u5f02\u5e38<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/errors.html#tut-exceptions\"><\/a><\/h2>\n\n\n\n<p>\u5c3d\u7ba1\u8bed\u53e5\u6216\u8868\u8fbe\u5f0f\u8bed\u6cd5\u4e0a\u662f\u6ca1\u6709\u95ee\u9898\u7684, \u5b83\u540c\u6837\u4e5f\u4f1a\u5728\u5c1d\u8bd5\u8fd0\u884c\u65f6\u5bfc\u81f4\u4e00\u4e2a\u9519\u8bef. \u5728\u6267\u884c\u65f6\u63a2\u6d4b\u5230\u7684\u9519\u8bef\u88ab\u6210\u4e3a\u00a0<em>exception<\/em>\u00a0, \u4e5f\u5c31\u662f\u5f02\u5e38, \u4f46\u5b83\u5e76\u4e0d\u662f\u81f4\u547d\u7684\u95ee\u9898: \u4f60\u5c06\u4f1a\u5f88\u5feb\u5b66\u5230\u5982\u4f55\u5728 Python \u7a0b\u5e8f\u4e2d\u5904\u7406\u5b83\u4eec. \u5927\u591a\u6570\u5f02\u5e38\u5e76\u4e0d\u4f1a\u88ab\u7a0b\u5e8f\u5904\u7406, \u4e0d\u8fc7, \u5bfc\u81f4\u9519\u8bef\u7684\u4fe1\u606f\u4f1a\u88ab\u663e\u793a\u51fa\u6765:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>10 * (1\/0)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Traceback (most recent call last): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">File \u201c&lt;stdin>\u201d, line 1, in ?<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> ZeroDivisionError: int division or modulo by zero<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>4 + spam*3<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Traceback (most recent call last):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> File \u201c&lt;stdin>\u201d, line 1, in ?<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> NameError: name \u2018spam\u2019 is not defined\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>\u20182\u2019 + 2<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Traceback (most recent call last): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">File \u201c&lt;stdin>\u201d, line 1, in ?<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> TypeError: Can\u2019t convert \u2018int\u2019 object to str implicitly<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u9519\u8bef\u4fe1\u606f\u7684\u6700\u540e\u4e00\u884c\u6216\u8bf4\u660e\u53d1\u751f\u4e86\u4ec0\u4e48. \u5f02\u5e38\u4f1a\u6709\u5f88\u591a\u7684\u7c7b\u578b, \u800c\u8fd9\u4e2a\u7c7b\u578b\u4f1a\u4f5c\u4e3a\u6d88\u606f\u7684\u4e00\u90e8\u5206\u6253\u5370\u51fa\u6765: \u5728\u6b64\u5904\u7684\u4f8b\u5b50\u4e2d\u7684\u7c7b\u578b\u6709&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#ZeroDivisionError\"><code>ZeroDivisionError<\/code><\/a>,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#NameError\"><code>NameError<\/code><\/a>&nbsp;\u548c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#TypeError\"><code>TypeError<\/code><\/a>. \u4f5c\u4e3a\u5f02\u5e38\u7c7b\u578b\u88ab\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u5176\u5b9e\u662f\u53d1\u751f\u5185\u5efa\u5f02\u5e38\u7684\u540d\u79f0. \u5bf9\u4e8e\u6240\u6709\u5185\u5efa\u5f02\u5e38\u90fd\u662f\u90a3\u6837\u7684, \u4f46\u662f\u5bf9\u4e8e\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38, \u5219\u53ef\u80fd\u4e0d\u662f\u8fd9\u6837 (\u5c3d\u7ba1\u6709\u67d0\u4e9b\u7ea6\u5b9a). \u6807\u51c6\u5f02\u5e38\u7684\u540d\u5b57\u662f\u5185\u5efa\u7684\u6807\u8bc6\u7b26 (\u4f46\u5e76\u4e0d\u662f\u5173\u952e\u5b57).<\/p>\n\n\n\n<p>\u6539\u884c\u5269\u4e0b\u7684\u90e8\u5206\u5219\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f, \u662f\u4ec0\u4e48\u6837\u7684\u5f02\u5e38, \u662f\u600e\u4e48\u5bfc\u81f4\u7684.<\/p>\n\n\n\n<p>\u9519\u8bef\u6d88\u606f\u7684\u524d\u9762\u90e8\u5206\u6307\u51fa\u4e86\u5f02\u5e38\u53d1\u751f\u7684\u4e0a\u4e0b\u6587, \u4ee5 stack traceback (\u6808\u8ffd\u8e2a) \u7684\u65b9\u5f0f\u663e\u793a. \u4e00\u822c\u6765\u8bf4\u5217\u51fa\u4e86\u6e90\u4ee3\u7801\u7684\u884c\u6570; \u4f46\u662f\u5e76\u4e0d\u4f1a\u663e\u793a\u4ece\u6807\u51c6\u8f93\u5165\u5f97\u5230\u7684\u884c\u6570.<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#bltin-exceptions\">Built-in Exceptions<\/a>&nbsp;\u5217\u51fa\u4e86\u5185\u5efa\u7684\u5f02\u5e38\u548c\u5b83\u4eec\u7684\u610f\u4e49.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8.3. \u5904\u7406\u5f02\u5e38<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/errors.html#tut-handling\"><\/a><\/h2>\n\n\n\n<p>\u5199\u7a0b\u5e8f\u6765\u5904\u7406\u5f02\u5e38\u662f\u53ef\u80fd\u7684. \u770b\u770b\u4e0b\u9762\u7684\u4f8b\u5b50, \u5b83\u8bf7\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5408\u6cd5\u7684\u6574\u6570, \u4f46\u662f\u4e5f\u5141\u8bb8\u7528\u6237\u6765\u4e2d\u65ad\u7a0b\u5e8f (\u4f7f\u7528\u00a0<code>Control-C<\/code>\u00a0\u6216\u4efb\u4f55\u64cd\u4f5c\u7cfb\u7edf\u652f\u6301\u7684); \u6ce8\u610f, \u7528\u6237\u751f\u6210\u7684\u4e2d\u65ad\u662f\u901a\u8fc7\u4ea7\u751f\u5f02\u5e38\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#KeyboardInterrupt\"><code>KeyboardInterrupt<\/code><\/a>:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0whileTrue<\/strong>:\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0try<\/strong>:\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0x = int(input(\u201cPlease enter a number: \u201c))\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0break<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0except<\/strong>\u00a0ValueError:\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(\u201cOops! That was no valid number. Try again\u2026\u201d)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>&nbsp;\u8bed\u53e5\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9996\u5148,&nbsp;<em>try clause<\/em>&nbsp;(\u5728&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>&nbsp;\u548c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#except\"><code>except<\/code><\/a>&nbsp;\u4e4b\u95f4\u7684\u8bed\u53e5) \u5c06\u88ab\u6267\u884c.<\/li>\n\n\n\n<li>\u5982\u679c\u6ca1\u6709\u5f02\u5e38\u53d1\u751f,&nbsp;<em>except clause<\/em>&nbsp;\u5c06\u88ab\u8df3\u8fc7,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>&nbsp;\u8bed\u53e5\u5c31\u7b97\u6267\u884c\u5b8c\u4e86.<\/li>\n\n\n\n<li>\u5982\u679c\u5728 try \u8bed\u53e5\u6267\u884c\u65f6, \u51fa\u73b0\u4e86\u4e00\u4e2a\u5f02\u5e38, \u8be5\u8bed\u53e5\u7684\u5269\u4e0b\u90e8\u5206\u5c06\u88ab\u8df3\u8fc7. \u7136\u540e\u5982\u679c\u5b83\u7684\u7c7b\u578b\u5339\u914d\u5230\u4e86&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#except\"><code>except<\/code><\/a>&nbsp;\u540e\u9762\u7684\u5f02\u5e38\u540d, \u90a3\u4e48\u8be5\u5f02\u5e38\u7684\u8bed\u53e5\u5c06\u88ab\u6267\u884c, \u800c\u6267\u884c\u5b8c\u540e\u4f1a\u8fd0\u884c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>&nbsp;\u540e\u9762\u7684\u95ee\u9898.<\/li>\n\n\n\n<li>\u5982\u679c\u4e00\u4e2a\u5f02\u5e38\u53d1\u751f\u65f6\u5e76\u6ca1\u6709\u5339\u914d\u5230 except \u8bed\u53e5\u4e2d\u7684\u5f02\u5e38\u540d, \u90a3\u4e48\u5b83\u5c31\u88ab\u4f20\u5230&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>&nbsp;\u8bed\u53e5\u5916\u9762; \u5982\u679c\u6ca1\u6709\u5904\u7406, \u90a3\u4e48\u5b83\u5c31\u662f&nbsp;<em>unhandled exception<\/em>&nbsp;\u5e76\u4e14\u5c06\u4f1a\u50cf\u524d\u9762\u90a3\u6837\u7ed9\u51fa\u4e00\u4e2a\u6d88\u606f\u7136\u540e\u6267\u884c.<\/li>\n<\/ul>\n\n\n\n<p>\u4e00\u4e2a\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>\u00a0\u8bed\u53e5\u53ef\u4ee5\u6709\u591a\u4e8e\u4e00\u6761\u7684 except \u8bed\u53e5, \u7528\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u5f02\u5e38. \u4f46\u81f3\u591a\u53ea\u6709\u4e00\u4e2a\u4f1a\u88ab\u6267\u884c. Handler \u4ec5\u4ec5\u5904\u7406\u5728\u76f8\u5e94 try \u8bed\u53e5\u4e2d\u7684\u5f02\u5e38, \u800c\u4e0d\u662f\u5728\u540c\u4e00\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>\u00a0\u8bed\u53e5\u4e2d\u7684\u5176\u4ed6 Handler. \u4e00\u4e2a\u5f02\u5e38\u7684\u8bed\u53e5\u53ef\u4ee5\u540c\u65f6\u5305\u62ec\u591a\u4e2a\u5f02\u5e38\u540d, \u4f46\u9700\u8981\u7528\u62ec\u53f7\u62ec\u8d77\u6765, \u6bd4\u5982:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u2026\u00a0<strong>except<\/strong>\u00a0(RuntimeError, TypeError, NameError):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u2026\u00a0<strong>pass<\/strong><\/p>\n\n\n\n<p>\u6700\u540e\u7684\u5f02\u5e38\u6bb5\u53ef\u4ee5\u5ffd\u7565\u5f02\u5e38\u7684\u540d\u5b57, \u7528\u4ee5\u5904\u7406\u5176\u4ed6\u7684\u60c5\u51b5. \u4f7f\u7528\u8fd9\u4e2a\u65f6\u9700\u8981\u7279\u522b\u6ce8\u610f, \u56e0\u4e3a\u5b83\u5f88\u5bb9\u6613\u5c4f\u853d\u4e86\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef! \u5b83\u4e5f\u7528\u4e8e\u8f93\u51fa\u9519\u8bef\u6d88\u606f, \u7136\u540e\u91cd\u65b0\u4ea7\u751f\u5f02\u5e38 (\u8ba9\u8c03\u7528\u8005\u5904\u7406\u8be5\u5f02\u5e38):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>importsys<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>try<\/strong>:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> f = open(\u2018myfile.txt\u2019) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">s = f.readline() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">i = int(s.strip())<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>except<\/strong>\u00a0IOError\u00a0<strong>as<\/strong>\u00a0err:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> print(\u201cI\/O error:\u00a0<em>{0}<\/em>\u201c.format(err))<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>except<\/strong>\u00a0ValueError:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> print(\u201cCould not convert data to an integer.\u201d)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>except<\/strong>:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> print(\u201cUnexpected error:\u201d, sys.exc_info()[0])\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>raise<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>\u00a0\u2026\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#except\"><code>except<\/code><\/a>\u00a0\u8bed\u53e5\u53ef\u4ee5\u6709\u4e00\u4e2a\u53ef\u9009\u7684\u00a0<em>else<\/em>\u00a0\u8bed\u53e5, \u5728\u8fd9\u91cc, \u5fc5\u987b\u8981\u653e\u5728\u6240\u6709 except \u8bed\u53e5\u540e\u9762. \u5b83\u5e38\u7528\u4e8e\u6ca1\u6709\u4ea7\u751f\u5f02\u5e38\u65f6\u5fc5\u987b\u6267\u884c\u7684\u8bed\u53e5. \u4f8b\u5982:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>for<\/strong>\u00a0arg\u00a0<strong>in<\/strong>\u00a0sys.argv[1:]:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>try<\/strong>:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> f = open(arg, \u2018r\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>except<\/strong>\u00a0IOError:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> print(\u2018cannot open\u2019, arg)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>else<\/strong>:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> print(arg, \u2018has\u2019, len(f.readlines()), \u2018lines\u2019)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> f.close()<\/p>\n\n\n\n<p>\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#else\"><code>else<\/code><\/a>&nbsp;\u6bd4\u989d\u5916\u7684\u6dfb\u52a0\u4ee3\u7801\u5230&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>&nbsp;\u4e2d\u8981\u597d, \u56e0\u4e3a\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5076\u7136\u7684\u6355\u83b7\u4e00\u4e2a\u5f02\u5e38, \u4f46\u5374\u4e0d\u662f\u7531\u4e8e\u6211\u4eec\u4fdd\u62a4\u7684\u4ee3\u7801\u6240\u629b\u51fa\u7684.<\/p>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u5f02\u5e38\u53d1\u751f\u4e86, \u5b83\u53ef\u80fd\u6709\u76f8\u5173\u7684\u503c, \u8fd9\u4e5f\u5c31\u662f\u6240\u8c13\u7684\u5f02\u5e38\u7684\u53c2\u6570. \u8be5\u53c2\u6570\u662f\u5426\u51fa\u73b0\u53ca\u5176\u7c7b\u578b\u4f9d\u8d56\u4e8e\u5f02\u5e38\u7684\u7c7b\u578b.<\/p>\n\n\n\n<p>\u5728 except \u8bed\u53e5\u4e2d\u53ef\u4ee5\u5728\u5f02\u5e38\u540d\u540e\u6307\u5b9a\u4e00\u4e2a\u53d8\u91cf. \u53d8\u91cf\u4f1a\u7ed1\u5b9a\u503c\u8fd9\u4e2a\u5f02\u5e38\u7684\u5b9e\u4f8b\u4e0a, \u5e76\u4e14\u628a\u53c2\u6570\u5b58\u4e8e\u00a0<code>instance.args<\/code>. \u4e3a\u4e86\u65b9\u4fbf, \u5f02\u5e38\u7684\u5b9e\u4f8b\u4f1a\u5b9a\u4e49\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/datamodel.html#object.__str__\"><code>__str__()<\/code><\/a>\u00a0\u6765\u76f4\u63a5\u5c06\u53c2\u6570\u6253\u5370\u51fa\u6765, \u800c\u4e0d\u7528\u5f15\u7528\u00a0<code>.args<\/code>. \u5f53\u7136\u4e5f\u53ef\u4ee5\u5728\u4ea7\u751f\u5f02\u5e38\u524d, \u9996\u5148\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5f02\u5e38, \u7136\u540e\u628a\u9700\u8981\u7684\u5c5e\u6027\u7ed1\u5b9a\u7ed9\u5b83.<\/p>\n\n\n\n<p><strong>>>>\u00a0try<\/strong>:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026\u00a0raise<\/strong>\u00a0Exception(\u2018spam\u2019, \u2018eggs\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0except<\/strong>\u00a0Exception\u00a0<strong>as<\/strong>\u00a0inst:\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(type(inst))\u00a0<em># the exception instance<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(inst.args)\u00a0<em># arguments stored in .args<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(inst)\u00a0<em># __str__ allows args to be printed directly,<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0<\/strong><em># but may be overridden in exception subclasses<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0x, y = inst.args\u00a0<em># unpack args<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(\u2018x =\u2019, x)\u00a0<strong>\u2026<\/strong>\u00a0print(\u2018y =\u2019, y)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0&lt;class \u2018Exception\u2019><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> (\u2018spam\u2019, \u2018eggs\u2019) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">(\u2018spam\u2019, \u2018eggs\u2019) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">x = spam <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">y = eggs<\/p>\n\n\n\n<p>\u5982\u679c\u4e00\u4e2a\u5f02\u5e38\u6709\u53c2\u6570, \u5b83\u4eec\u5c06\u4f5c\u4e3a\u5f02\u5e38\u6d88\u606f\u7684\u6700\u540e\u4e00\u90e8\u5206\u6253\u5370\u51fa\u6765.<\/p>\n\n\n\n<p>\u5f02\u5e38\u7684 handler \u5904\u7406\u7684\u5f02\u5e38, \u4e0d\u4ec5\u4ec5\u662f try \u8bed\u53e5\u4e2d\u90a3\u4e9b\u76f4\u63a5\u7684\u5f02\u5e38, \u4e5f\u53ef\u4ee5\u662f\u5728\u6b64\u5904\u8c03\u7528\u7684\u51fd\u6570\u6240\u4ea7\u751f\u7684\u5f02\u5e38. \u4f8b\u5982:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0def<\/strong>\u00a0this_fails():\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0x = 1\/0\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0try<\/strong>:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026<\/strong>\u00a0this_fails()<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026\u00a0except<\/strong>\u00a0ZeroDivisionError\u00a0<strong>as<\/strong>\u00a0err:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026<\/strong>\u00a0print(\u2018Handling run-time error:\u2019, err)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Handling run-time error: int division or modulo by zero<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8.4. \u629b\u51fa\u5f02\u5e38<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/errors.html#tut-raising\"><\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#raise\"><code>raise<\/code><\/a>\u00a0\u8bed\u53e5\u5141\u8bb8\u7a0b\u5e8f\u5458\u5f3a\u5236\u4e00\u4e2a\u7279\u5b9a\u7684\u5f02\u5e38\u7684\u53d1\u751f. \u4e3e\u4e2a\u4f8b\u5b50:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0raise<\/strong>\u00a0NameError(\u2018HiThere\u2019) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Traceback (most recent call last): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">File \u201c&lt;stdin>\u201d, line 1, in ?<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> NameError: HiThere<\/p>\n\n\n\n<p>\u7ed9&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#raise\"><code>raise<\/code><\/a>&nbsp;\u7684\u552f\u4e00\u53c2\u6570\u8868\u793a\u4ea7\u751f\u7684\u5f02\u5e38. \u8fd9\u5fc5\u987b\u662f\u4e00\u4e2a\u5f02\u5e38\u5b9e\u4f8b\u6216\u7c7b (\u6d3e\u751f\u81ea&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#Exception\"><code>Exception<\/code><\/a>&nbsp;\u7684\u7c7b).<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u51b3\u5b9a\u4ea7\u751f\u4e00\u4e2a\u5f02\u5e38, \u4f46\u662f\u4e0d\u51c6\u5907\u5904\u7406\u5b83, \u90a3\u4e48\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f, \u91cd\u65b0\u629b\u51fa\u5f02\u5e38:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0try<\/strong>:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026\u00a0raise<\/strong>\u00a0NameError(\u2018HiThere\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0except<\/strong>\u00a0NameError:\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(\u2018An exception flew by!\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0raise<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">An exception flew by! <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Traceback (most recent call last): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">File \u201c&lt;stdin>\u201d, line 2, in ?<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> NameError: HiThere<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8.5. \u81ea\u5b9a\u4e49\u5f02\u5e38<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/errors.html#tut-userexceptions\"><\/a><\/h2>\n\n\n\n<p>\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u5f02\u5e38\u7c7b (\u66f4\u591a\u7684\u7c7b\u8bf7\u53c2\u8003\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-classes\">\u7c7b<\/a>) \u6765\u547d\u540d\u5b83\u4eec\u81ea\u5df1\u7684\u5f02\u5e38. \u5f02\u5e38\u9700\u8981\u4ece\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#Exception\"><code>Exception<\/code><\/a>\u00a0\u7c7b\u6d3e\u751f, \u65e2\u53ef\u4ee5\u662f\u76f4\u63a5\u4e5f\u53ef\u4ee5\u662f\u95f4\u63a5. \u4f8b\u5982:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0classMyError<\/strong>(Exception):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026\u00a0def<\/strong>\u00a0__init__(self, value):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026<\/strong>\u00a0self.value = value\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0def<\/strong>\u00a0__str__(self):\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0return<\/strong>\u00a0repr(self.value)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0try<\/strong>:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026\u00a0raise<\/strong>\u00a0MyError(2*2)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0except<\/strong>\u00a0MyError\u00a0<strong>as<\/strong>\u00a0e:\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(\u2018My exception occurred, value:\u2019, e.value)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">My exception occurred, value: 4<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0raise<\/strong>\u00a0MyError(\u2018oops!\u2019)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Traceback (most recent call last):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> File \u201c&lt;stdin>\u201d, line 1, in ?<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> __main__.MyError: \u2018oops!\u2019<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d,\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#Exception\"><code>Exception<\/code><\/a>\u00a0\u7684\u9ed8\u8ba4\u65b9\u6cd5\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/datamodel.html#object.__init__\"><code>__init__()<\/code><\/a>\u00a0\u88ab\u8986\u5199\u4e86. \u73b0\u5728\u65b0\u7684\u5f02\u5e38\u7c7b\u53ef\u4ee5\u50cf\u5176\u4ed6\u7684\u7c7b\u4e00\u6837\u505a\u4efb\u4f55\u7684\u4e8b, \u4f46\u662f\u5e38\u5e38\u4f1a\u4fdd\u6301\u7b80\u5355\u6027, \u4ec5\u4ec5\u63d0\u4f9b\u4e00\u4e9b\u53ef\u4ee5\u88ab handler \u5904\u7406\u7684\u5f02\u5e38\u4fe1\u606f. \u5f53\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u65f6, \u53ef\u80fd\u4f1a\u6709\u591a\u79cd\u4e0d\u540c\u7684\u5f02\u5e38, \u4e00\u79cd\u5e38\u7528\u7684\u505a\u6cd5\u5c31\u662f, \u521b\u5efa\u4e00\u4e2a\u57fa\u7c7b, \u7136\u540e\u6d3e\u751f\u51fa\u5404\u79cd\u4e0d\u540c\u7684\u5f02\u5e38:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"390\" src=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-7-1024x390.png\" alt=\"\" class=\"wp-image-384\" srcset=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-7-1024x390.png 1024w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-7-300x114.png 300w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-7-768x292.png 768w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-7.png 1364w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u5927\u591a\u6570\u5f02\u5e38\u5b9a\u4e49\u65f6\u90fd\u4f1a\u4ee5 \u201cError\u201d \u7ed3\u5c3e, \u5c31\u50cf\u6807\u51c6\u5f02\u5e38\u7684\u547d\u540d.<\/p>\n\n\n\n<p>\u5927\u591a\u6570\u6807\u51c6\u6a21\u5757\u90fd\u5b9a\u4e49\u4e86\u5b83\u4eec\u81ea\u5df1\u7684\u5f02\u5e38, \u7528\u4e8e\u62a5\u544a\u5728\u5b83\u4eec\u5b9a\u4e49\u7684\u51fd\u6570\u4e2d\u53d1\u751f\u7684\u9519\u8bef. \u5173\u4e8e\u66f4\u591a\u7c7b\u7684\u4fe1\u606f\u8bf7\u53c2\u8003&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/classes.html#tut-classes\">\u7c7b<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8.6. \u5b9a\u4e49\u6e05\u7406\u52a8\u4f5c<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/errors.html#tut-cleanup\"><\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>\u00a0\u8bed\u53e5\u6709\u53e6\u4e00\u79cd\u53ef\u9009\u7684\u4ece\u53e5, \u7528\u4e8e\u5b9a\u4e49\u4e00\u4e9b\u626b\u5c3e\u7684\u5de5\u4f5c, \u6b64\u5904\u5b9a\u4e49\u7684\u8bed\u53e5\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4f1a\u88ab\u6267\u884c. \u4f8b\u5982:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0try<\/strong>:\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0raise<\/strong>\u00a0KeyboardInterrupt\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026\u00a0finally<\/strong>:\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(\u2018Goodbye, world!\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0Goodbye, world!<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>KeyboardInterrupt<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0Traceback (most recent call last):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> File \u201c&lt;stdin>\u201d, line 2, in ?<\/p>\n\n\n\n<p>\u4e00\u4e2a\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/errors.html#id14\">*<\/a>finally \u8bed\u53e5*\u603b\u662f\u5728\u79bb\u5f00\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>\u00a0\u8bed\u53e5\u524d\u88ab\u6267\u884c, \u800c\u65e0\u8bba\u6b64\u5904\u6709\u65e0\u5f02\u5e38\u53d1\u751f. \u5f53\u4e00\u4e2a\u5f02\u5e38\u5728\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>\u00a0\u4e2d\u4ea7\u751f, \u4f46\u662f\u5e76\u6ca1\u6709\u88ab\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#except\"><code>except<\/code><\/a>\u00a0\u5904\u7406 (\u6216\u8005\u5b83\u53d1\u751f\u5728\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#except\"><code>except<\/code><\/a>\u00a0\u6216\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#else\"><code>else<\/code><\/a>\u00a0\u8bed\u53e5\u4e2d), \u90a3\u4e48\u5728\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#finally\"><code>finally<\/code><\/a>\u00a0\u8bed\u53e5\u6267\u884c\u540e\u4f1a\u88ab\u91cd\u65b0\u629b\u51fa.\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#finally\"><code>finally<\/code><\/a>\u00a0\u8bed\u53e5\u5728\u5176\u4ed6\u8bed\u53e5\u8981\u9000\u51fa\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>\u00a0\u65f6\u4e5f\u4f1a\u88ab\u6267\u884c, \u50cf\u662f\u4f7f\u7528\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#break\"><code>break<\/code><\/a>,\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#continue\"><code>continue<\/code><\/a>\u00a0\u6216\u8005\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/simple_stmts.html#return\"><code>return<\/code><\/a>. \u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"591\" height=\"385\" src=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-8.png\" alt=\"\" class=\"wp-image-386\" srcset=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-8.png 591w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-8-300x195.png 300w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/><\/figure>\n\n\n\n<p>\u6b63\u5982\u4f60\u6240\u770b\u5230\u7684,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#finally\"><code>finally<\/code><\/a>&nbsp;\u8bed\u53e5\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u88ab\u6267\u884c\u4e86. \u7531\u4e8e\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u76f8\u9664\u800c\u4ea7\u751f\u7684&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#TypeError\"><code>TypeError<\/code><\/a>&nbsp;\u5e76\u6ca1\u6709\u88ab&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#except\"><code>except<\/code><\/a>&nbsp;\u8bed\u53e5\u5904\u7406, \u56e0\u6b64\u5728\u6267\u884c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#finally\"><code>finally<\/code><\/a>&nbsp;\u540e\u88ab\u91cd\u65b0\u629b\u51fa.<\/p>\n\n\n\n<p>\u5728\u771f\u6b63\u7684\u5e94\u7528\u4e2d,&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#finally\"><code>finally<\/code><\/a>&nbsp;\u662f\u975e\u5e38\u6709\u7528\u7684, \u7279\u522b\u662f\u91ca\u653e\u989d\u5916\u7684\u8d44\u6e90 (\u7c7b\u4f3c\u6587\u4ef6\u6216\u7f51\u7edc\u8fde\u63a5), \u65e0\u8bba\u6b64\u8d44\u6e90\u662f\u5426\u6210\u529f\u4f7f\u7528.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8.7. \u9884\u5b9a\u4e49\u7684\u6e05\u7406\u52a8\u4f5c<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/errors.html#tut-cleanup-with\"><\/a><\/h2>\n\n\n\n<p>\u6709\u4e9b\u5bf9\u8c61\u5b9a\u4e49\u4e86\u6807\u51c6\u7684\u6e05\u7406\u5de5\u4f5c, \u7279\u522b\u662f\u5bf9\u8c61\u4e0d\u518d\u9700\u8981\u65f6, \u65e0\u8bba\u5bf9\u5176\u4f7f\u7528\u7684\u64cd\u4f5c\u662f\u5426\u6210\u529f. \u770b\u770b\u4e0b\u9762\u7684\u4f8b\u5b50, \u5b83\u5c1d\u8bd5\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u5e76\u8f93\u51fa\u5185\u5bb9\u81f3\u5c4f\u5e55.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>for<\/strong>\u00a0line\u00a0<strong>in<\/strong>\u00a0open(\u201cmyfile.txt\u201d):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> print(line)<\/p>\n\n\n\n<p>\u524d\u9762\u8fd9\u6bb5\u4ee3\u7801\u7684\u95ee\u9898\u5728\u4e8e, \u5728\u6b64\u4ee3\u7801\u6210\u529f\u6267\u884c\u540e, \u6587\u4ef6\u4f9d\u7136\u88ab\u6253\u5f00\u7740. \u5728\u7b80\u5355\u7684\u811a\u672c\u4e2d\u8fd9\u53ef\u80fd\u4e0d\u662f\u4ec0\u4e48\u95ee\u9898, \u4f46\u662f\u5bf9\u4e8e\u66f4\u5927\u7684\u5e94\u7528\u6765\u8bf4\u5374\u662f\u4e2a\u95ee\u9898.\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/reference\/compound_stmts.html#with\"><code>with<\/code><\/a>\u00a0\u8bed\u53e5\u5c31\u5141\u8bb8\u50cf\u6587\u4ef6\u8fd9\u6837\u7684\u5bf9\u8c61\u5728\u4f7f\u7528\u540e\u4f1a\u88ab\u6b63\u5e38\u7684\u6e05\u7406\u6389.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>with<\/strong>\u00a0open(\u201cmyfile.txt\u201d)\u00a0<strong>as<\/strong>\u00a0f:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>for<\/strong>\u00a0line\u00a0<strong>in<\/strong>\u00a0f:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> print(line)<\/p>\n\n\n\n<p>\u5728\u6267\u884c\u8be5\u8bed\u53e5\u540e, \u6587\u4ef6&nbsp;<em>f<\/em>&nbsp;\u5c31\u4f1a\u88ab\u5173\u95ed, \u5c31\u7b97\u662f\u5728\u8bfb\u53d6\u65f6\u78b0\u5230\u4e86\u95ee\u9898. \u50cf\u6587\u4ef6\u8fd9\u6837\u7684\u5bf9\u8c61, \u603b\u4f1a\u63d0\u4f9b\u9884\u5b9a\u4e49\u7684\u6e05\u7406\u5de5\u4f5c, \u66f4\u591a\u7684\u53ef\u4ee5\u53c2\u8003\u5b83\u4eec\u7684\u6587\u6863.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5230\u73b0\u5728\u4e3a\u6b62, \u6ca1\u6709\u66f4\u591a\u7684\u63d0\u53ca\u9519\u8bef\u4fe1\u606f, \u4f46\u662f\u5f53\u4f60\u5728\u5c1d\u8bd5\u8fd9\u4e9b\u4f8b\u5b50\u65f6, \u6216\u591a\u6216\u5c11\u4f1a\u78b0\u5230\u4e00\u4e9b. \u8fd9\u91cc (\u81f3\u5c11) \u6709\u4e24 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-271","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":"","_links":{"self":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=271"}],"version-history":[{"count":8,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"predecessor-version":[{"id":388,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/271\/revisions\/388"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}