{"id":92,"date":"2026-08-06T00:28:58","date_gmt":"2026-08-05T16:28:58","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=92"},"modified":"2026-08-06T00:44:14","modified_gmt":"2026-08-05T16:44:14","slug":"day-13-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%ef%bc%9atry-except-else-finally-%e4%b8%8e-raise","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/06\/day-13-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%ef%bc%9atry-except-else-finally-%e4%b8%8e-raise\/","title":{"rendered":"| Day 13 | \u5f02\u5e38\u5904\u7406\uff1atry \/ except \/ else \/ finally \u4e0e raise |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 \u5f02\u5e38\u662f\u4ec0\u4e48 \u2014\u2014 \u7a0b\u5e8f\u51fa\u9519\u65f6\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5f02\u5e38<\/strong>\uff08exception\uff09\uff1a\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53d1\u751f\u7684\u300c\u610f\u5916\u4e8b\u4ef6\u300d\uff0c\u6bd4\u5982\u9664\u4ee5 0\u3001\u8bfb\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u3001\u628a <code>\"abc\"<\/code> \u8f6c\u6210\u6570\u5b57\u3002Python \u9047\u5230\u5f02\u5e38\u65f6\u4f1a<strong>\u7acb\u5373\u4e2d\u65ad\u5f53\u524d\u4ee3\u7801<\/strong>\uff0c\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\u4e00\u8def\u5411\u4e0a\u4f20\u9012\uff1b\u6ca1\u4eba\u63a5\u4f4f\u5b83\uff0c\u7a0b\u5e8f\u5c31\u5d29\u6e83\u9000\u51fa\u3002\u5b66\u4f1a\u770b\u5f02\u5e38\u3001\u8ba4\u8bc6\u5f02\u5e38\uff0c\u662f\u5199\u4efb\u4f55\u771f\u5b9e\u7a0b\u5e8f\u7684\u7b2c\u4e00\u6b65\u2014\u2014\u62a5\u9519\u4e0d\u662f\u654c\u4eba\uff0c\u5b83\u662f\u5728\u7528\u7cbe\u786e\u7684\u8bed\u8a00\u544a\u8bc9\u4f60\u300c\u54ea\u91cc\u51fa\u4e86\u95ee\u9898\u300d\u3002\u7a0b\u5e8f\u6700\u6015\u7684\u4e0d\u662f\u62a5\u9519\uff0c\u800c\u662f\u62a5\u9519\u540e\u76f4\u63a5\u5d29\u6e83\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a0b\u5e8f\u6267\u884c\u65f6\uff0cPython \u89e3\u91ca\u5668\u53d1\u73b0\u67d0\u6761\u8bed\u53e5\u65e0\u6cd5\u6b63\u5e38\u6267\u884c\uff0c\u5c31<strong>\u521b\u5efa\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61<\/strong>\uff08\u5982 <code>ZeroDivisionError(\"division by zero\")<\/code>\uff09\u5e76\u300c\u629b\u300d\u51fa\u53bb\u3002\u5f02\u5e38\u6cbf\u7740\u8c03\u7528\u6808\uff08\u51fd\u6570\u5c42\u5c42\u8c03\u7528\u5f62\u6210\u7684\u8def\u5f84\uff09\u5411\u4e0a\u4f20\u9012\uff1a\u4e2d\u9014\u6709 <code>except<\/code> \u63a5\u4f4f\uff0c\u7a0b\u5e8f\u7ee7\u7eed\u8fd0\u884c\uff1b\u4e00\u76f4\u6ca1\u4eba\u63a5\uff0c\u89e3\u91ca\u5668\u6253\u5370 Traceback\uff08\u56de\u6eaf\u4fe1\u606f\uff09\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u3002<strong>\u5f02\u5e38\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u5bf9\u8c61<\/strong>\u2014\u2014\u6709\u7c7b\u578b\u3001\u6709\u63cf\u8ff0\u3001\u6709\u7ee7\u627f\u4f53\u7cfb\uff08<code>ZeroDivisionError<\/code> \u2192 <code>ArithmeticError<\/code> \u2192 <code>Exception<\/code> \u2192 <code>BaseException<\/code>\uff0c\u6240\u4ee5\u6355\u83b7 <code>Exception<\/code> \u80fd\u63a5\u4f4f\u6240\u6709\u666e\u901a\u5f02\u5e38\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f02\u5e38\u50cf<strong>\u9910\u5385\u540e\u53a8\u7684\u4f20\u83dc\u94c3<\/strong>\uff1a\u53a8\u5e08\u53d1\u73b0\u83dc\u505a\u4e0d\u4e86\uff08\u98df\u6750\u6ca1\u4e86\uff09\uff0c\u4e0d\u662f\u628a\u6574\u4e2a\u9910\u5385\u5173\u95e8\uff0c\u800c\u662f\u6309\u94c3\u628a\u95ee\u9898\u300c\u4f20\u300d\u51fa\u53bb\u2014\u2014\u670d\u52a1\u5458\uff08<code>except<\/code>\uff09\u542c\u5230\u94c3\u58f0\u5c31\u6765\u5904\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u5f02\u5e38\u662f\u4e00\u4e2a\u300c\u5bf9\u8c61\u300d\u2014\u2014\u628a\u5b83\u6293\u51fa\u6765\u770b\u4e2a\u6e05\u695a<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    result = 10 \/ 0\nexcept ZeroDivisionError as e:\n    print(\"\u5f02\u5e38\u7c7b\u578b\uff1a\", type(e).__name__)\n    print(\"\u5f02\u5e38\u5185\u5bb9\uff1a\", e)\n    print(\"\u662f\u5426\u5f02\u5e38\u5bf9\u8c61\uff1a\", isinstance(e, Exception))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5f02\u5e38\u7c7b\u578b\uff1a ZeroDivisionError\n\u5f02\u5e38\u5185\u5bb9\uff1a division by zero\n\u662f\u5426\u5f02\u5e38\u5bf9\u8c61\uff1a True\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>10 \/ 0<\/code> \u89e6\u53d1\u5f02\u5e38\uff0c\u88ab <code>except ZeroDivisionError as e<\/code> \u63a5\u4f4f\uff0c\u5f02\u5e38\u5bf9\u8c61\u5b58\u8fdb\u53d8\u91cf <code>e<\/code>\u3002<code>type(e).__name__<\/code> \u6253\u5370\u7c7b\u578b\u540d\uff1b<code>isinstance(e, Exception)<\/code> \u9a8c\u8bc1\u5b83\u662f <code>Exception<\/code> \u5bb6\u65cf\u7684\u5bf9\u8c61\u3002<strong>\u8bb0\u4f4f\uff1a\u5f02\u5e38\u4e0d\u662f\u62bd\u8c61\u6982\u5ff5\uff0c\u662f\u770b\u5f97\u89c1\u6478\u5f97\u7740\u7684\u5bf9\u8c61\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u6ca1\u4eba\u63a5\u4f4f\u5f02\u5e38\u65f6\uff0c\u7a0b\u5e8f\u957f\u4ec0\u4e48\u6837\uff08\u9519\u8bef\u793a\u8303\uff0c\u4e0d\u8981\u8fd0\u884c\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># print(10 \/ 0)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff08\u7a0b\u5e8f\u7ec8\u6b62\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Traceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nZeroDivisionError: division by zero\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u5230 Traceback \u4e0d\u8981\u614c\uff0c\u5b83\u6709\u4e09\u6bb5\u4fe1\u606f\uff1a<strong>\u51fa\u9519\u4f4d\u7f6e<\/strong>\uff08File&#8230;line 1\uff09\u2192 <strong>\u5f02\u5e38\u7c7b\u578b<\/strong>\uff08ZeroDivisionError\uff09\u2192 <strong>\u5177\u4f53\u539f\u56e0<\/strong>\uff08division by zero\uff09\u3002\u8bfb\u61c2\u8fd9\u4e09\u6bb5\uff0c\u5c31\u80fd\u5b9a\u4f4d 90% \u7684 bug\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u770b\u5230\u62a5\u9519\u5c31\u614c\uff0c\u628a Traceback \u5f53\u300c\u7a0b\u5e8f\u5e9f\u4e86\u300d\u7684\u4fe1\u53f7 \u2192 \u95ee\u9898\uff1a\u9519\u8fc7\u4e86\u62a5\u9519\u91cc\u6700\u6709\u4ef7\u503c\u7684\u5b9a\u4f4d\u4fe1\u606f \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9010\u884c\u8bfb Traceback\uff1a\u4f4d\u7f6e \u2192 \u7c7b\u578b \u2192 \u539f\u56e0\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7528 <code>except:<\/code>\uff08\u4e0d\u5199\u7c7b\u578b\uff09\u4e00\u628a\u6293 \u2192 \u95ee\u9898\uff1a\u8fde <code>KeyboardInterrupt<\/code>\uff08Ctrl+C\uff09\u90fd\u541e\u6389\uff0c\u7a0b\u5e8f\u65e0\u6cd5\u505c\u6b62 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5199\u660e\u786e\u7c7b\u578b\uff0c\u6216\u6700\u591a\u7528 <code>except Exception<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u62a5\u9519\u4e0d\u662f\u574f\u4e8b\uff0c\u662f\u7a0b\u5e8f\u5728\u8bf4\u8bdd\uff1b\u4f4d\u7f6e\u3001\u7c7b\u578b\u3001\u539f\u56e0\uff0c\u4e09\u884c\u8bfb\u61c2\u5b83\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 try \/ except \u2014\u2014 \u628a\u300c\u53ef\u80fd\u51fa\u9519\u300d\u548c\u300c\u51fa\u9519\u600e\u4e48\u529e\u300d\u5206\u5f00<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>try<\/code> \u5757\u653e\u300c\u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801\u300d\uff0c<code>except<\/code> \u5757\u653e\u300c\u51fa\u9519\u540e\u600e\u4e48\u529e\u300d\u3002\u8fd9\u662f\u5f02\u5e38\u5904\u7406\u7684<strong>\u6838\u5fc3\u673a\u5236<\/strong>\uff1a\u8ba9\u300c\u6b63\u5e38\u903b\u8f91\u300d\u548c\u300c\u9519\u8bef\u5904\u7406\u300d\u5404\u5f52\u5176\u4f4d\u3002\u8ba9\u7a0b\u5e8f\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u300c\u5f53\u573a\u53bb\u4e16\u300d\uff0c\u800c\u662f\u4f18\u96c5\u5730\u63d0\u793a\u7528\u6237\u5e76\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>try<\/code> \u5757\u91cc\u7684\u8bed\u53e5\u88ab\u89e3\u91ca\u5668<strong>\u7279\u6b8a\u76d1\u63a7<\/strong>\uff1a\u4e00\u65e6\u67d0\u6761\u8bed\u53e5\u629b\u51fa\u5f02\u5e38\uff0c\u5757\u5185<strong>\u5269\u4f59\u8bed\u53e5\u7acb\u523b\u8df3\u8fc7<\/strong>\uff0c\u63a7\u5236\u6743\u76f4\u63a5\u8df3\u5230\u5339\u914d\u7684 <code>except<\/code>\u3002\u591a\u4e2a <code>except<\/code> \u4ece\u4e0a\u5230\u4e0b<strong>\u6309\u987a\u5e8f\u5339\u914d<\/strong>\u2014\u2014\u7b2c\u4e00\u4e2a\u300c\u80fd\u63a5\u4f4f\u8fd9\u4e2a\u5f02\u5e38\u300d\u7684\u751f\u6548\uff08\u5b50\u7c7b\u80fd\u88ab\u7236\u7c7b\u63a5\u4f4f\uff09\u3002<strong>except \u540e\u5199\u7684\u7c7b\u578b\u8d8a\u5177\u4f53\uff0c\u5339\u914d\u8d8a\u7cbe\u786e\uff1b\u8d8a\u9760\u4e0a\uff0c\u4f18\u5148\u7ea7\u8d8a\u9ad8<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">try\/except \u50cf<strong>\u5b89\u5168\u6c14\u56ca + \u5e94\u6025\u9884\u6848<\/strong>\uff1a\u6b63\u5e38\u884c\u9a76\u65f6\u6c14\u56ca\uff08\u6b63\u5e38\u4ee3\u7801\uff09\u4e0d\u5e72\u9884\uff1b\u4e00\u65e6\u649e\u8f66\uff08\u5f02\u5e38\u53d1\u751f\uff09\uff0c\u6c14\u56ca\u77ac\u95f4\u5f39\u51fa\uff08except \u63a5\u7ba1\uff09\uff0c\u8f66\u91cc\u7684\u4eba\u7ee7\u7eed\u5f00\u8f66\u3002\u591a\u4e2a except \u50cf\u591a\u5c42\u5b89\u5168\u7f51\uff1a\u7b2c\u4e00\u5c42\u63a5\u4e0d\u4f4f\u5c31\u843d\u5230\u7b2c\u4e8c\u5c42\u2014\u2014<strong>\u4ece\u4e0a\u5f80\u4e0b\u8bd5<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>try\/except \u7684\u57fa\u672c\u5f62\u6001\u2014\u2014\u51fa\u9519\u540e\u7a0b\u5e8f\u7ee7\u7eed\u8fd0\u884c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    num = int(\"abc\")\nexcept ValueError as e:\n    print(\"\u8f6c\u6362\u5931\u8d25\uff0c\u539f\u56e0\u662f\uff1a\", e)\nprint(\"\u7a0b\u5e8f\u7ee7\u7eed\u8fd0\u884c\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8f6c\u6362\u5931\u8d25\uff0c\u539f\u56e0\u662f\uff1a invalid literal for int() with base 10: 'abc'\n\u7a0b\u5e8f\u7ee7\u7eed\u8fd0\u884c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>int(\"abc\")<\/code> \u629b\u51fa <code>ValueError<\/code>\uff0c\u88ab except \u63a5\u4f4f\u5e76\u6253\u5370\u539f\u56e0\uff1b\u5173\u952e\u5728\u6700\u540e\u4e00\u884c\u2014\u2014<strong>except \u5904\u7406\u5b8c\u540e\uff0c\u7a0b\u5e8f\u4ece try \u5757\u4e4b\u540e\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c<\/strong>\uff0c\u6ca1\u6709\u5d29\u6e83\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u591a\u4e2a except \u6309\u987a\u5e8f\u5339\u914d\u2014\u2014\u5148\u5199\u5177\u4f53\u7684\uff0c\u518d\u5199\u7b3c\u7edf\u7684<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    num = int(\"abc\")\nexcept ValueError:\n    print(\"\u547d\u4e2d ValueError\uff1a\u4f60\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\")\nexcept TypeError:\n    print(\"\u547d\u4e2d TypeError\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u547d\u4e2d ValueError\uff1a\u4f60\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>int(\"abc\")<\/code> \u629b\u7684\u662f <code>ValueError<\/code>\uff0cPython \u4ece\u4e0a\u5230\u4e0b\u68c0\u67e5\uff1a\u7b2c\u4e00\u4e2a <code>except ValueError<\/code> \u80fd\u63a5\u4f4f\uff0c\u7acb\u523b\u751f\u6548\uff0c\u540e\u9762\u7684\u4e0d\u518d\u68c0\u67e5\u3002<strong>\u5982\u679c\u591a\u4e2a except \u90fd\u80fd\u63a5\u4f4f\u540c\u4e00\u4e2a\u5f02\u5e38\uff0c\u6c38\u8fdc\u662f\u6700\u9760\u4e0a\u7684\u751f\u6548<\/strong>\u2014\u2014\u6240\u4ee5\u300c\u5177\u4f53\u7684\u300d\u8981\u5199\u5728\u300c\u7b3c\u7edf\u7684\u300d\u4e0a\u9762\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>except Exception\u2014\u2014\u515c\u5e95\u7f51\u7684\u5de5\u4f5c\u539f\u7406\uff08\u7ee7\u627f\u94fe\u7684\u5e94\u7528\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    num = int(\"abc\")\nexcept Exception as e:\n    print(\"\u6355\u83b7\u5230\uff08Exception \u80fd\u63a5\u4f4f ValueError\uff09\uff1a\", type(e).__name__)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6355\u83b7\u5230\uff08Exception \u80fd\u63a5\u4f4f ValueError\uff09\uff1a ValueError\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ValueError<\/code> \u662f <code>Exception<\/code> \u7684\u5b50\u7c7b\uff0c\u6240\u4ee5 <code>except Exception<\/code> \u80fd\u63a5\u4f4f\u5b83\uff0c<code>type(e).__name__<\/code> \u4ecd\u80fd\u544a\u8bc9\u6211\u4eec\u771f\u5b9e\u7c7b\u578b\u3002**<code>except Exception<\/code> \u9002\u5408\u505a\u300c\u6700\u540e\u4e00\u5c42\u515c\u5e95\u7f51\u300d**\u2014\u2014\u63a5\u4f4f\u4e00\u5207\u666e\u901a\u5f02\u5e38\u5e76\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u88f8\u5199 <code>except:<\/code> \u2192 \u95ee\u9898\uff1a\u8fde Ctrl+C\uff08<code>KeyboardInterrupt<\/code>\uff09\u90fd\u88ab\u541e\u6389\uff0c\u7a0b\u5e8f\u6ca1\u6cd5\u505c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5199\u5177\u4f53\u7c7b\u578b\uff0c\u6216\u6700\u591a <code>except Exception<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u628a <code>except Exception<\/code> \u5199\u5728\u6700\u4e0a\u9762 \u2192 \u95ee\u9898\uff1a\u540e\u9762\u7684\u5177\u4f53 except \u6c38\u8fdc\u8f6e\u4e0d\u5230\uff0c\u767d\u5199 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5177\u4f53\u7684\u5199\u524d\u9762\uff0c<code>Exception<\/code> \u515c\u5e95\u5199\u6700\u540e\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1atry \u91cc\u653e\u9669\u6d3b\uff0cexcept \u6765\u63a5\u9505\uff1b\u5177\u4f53\u5728\u524d\uff0c\u515c\u5e95\u5728\u540e\uff0c\u987a\u5e8f\u51b3\u5b9a\u80dc\u8d1f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 else \u4e0e finally \u2014\u2014 \u300c\u6210\u529f\u4e4b\u540e\u300d\u4e0e\u300c\u65e0\u8bba\u6210\u8d25\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b8c\u6574\u7684\u5f02\u5e38\u5904\u7406\u7ed3\u6784\u6709\u56db\u90e8\u5206\uff1a<code>try<\/code>\uff08\u5c1d\u8bd5\uff09\u3001<code>except<\/code>\uff08\u51fa\u9519\uff09\u3001<code>else<\/code>\uff08\u6210\u529f\uff09\u3001<code>finally<\/code>\uff08\u65e0\u8bba\u6210\u8d25\uff09\u3002<code>else<\/code> \u53ea\u5728<strong>\u6ca1\u6709\u5f02\u5e38\u65f6<\/strong>\u6267\u884c\uff0c\u628a\u300c\u6210\u529f\u8def\u5f84\u300d\u4ece try \u91cc\u5355\u72ec\u62ce\u51fa\u6765\uff1b<code>finally<\/code> <strong>\u65e0\u8bba\u6709\u6ca1\u6709\u5f02\u5e38\u90fd\u6267\u884c<\/strong>\uff0c\u662f\u91ca\u653e\u8d44\u6e90\uff08\u5173\u6587\u4ef6\u3001\u5173\u8fde\u63a5\u3001\u91ca\u653e\u9501\uff09\u6700\u4fdd\u9669\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u987a\u5e8f\u662f\u786e\u5b9a\u7684\uff1a\u2460 \u6267\u884c <code>try<\/code> \u5757 \u2192 \u2461 \u82e5\u629b\u51fa\u5f02\u5e38\uff0c\u8df3\u5230\u5339\u914d\u7684 <code>except<\/code> \u2192 \u2462 \u82e5<strong>\u6ca1\u6709<\/strong>\u5f02\u5e38\uff0c\u6267\u884c <code>else<\/code> \u2192 \u2463 <strong>\u65e0\u8bba\u2461\u2462\u8d70\u54ea\u6761\u8def<\/strong>\uff0c\u6700\u540e\u90fd\u6267\u884c <code>finally<\/code>\u3002\u5173\u952e\u70b9\uff1a<code>else<\/code> \u548c <code>except<\/code> \u4e92\u65a5\uff08\u6709\u5f02\u5e38\u8d70 except\uff0c\u6ca1\u5f02\u5e38\u8d70 else\uff09\uff1b<code>finally<\/code> \u662f\u300c\u5fc5\u7ecf\u4e4b\u8def\u300d\u3002\u8fd8\u6709\u4e2a\u9690\u85cf\u7ec6\u8282\uff1a**<code>finally<\/code> \u5148\u4e8e <code>return<\/code> \u6267\u884c**\u2014\u2014\u51fd\u6570\u91cc try \u5757\u5373\u4f7f\u5199\u4e86 <code>return<\/code>\uff0c\u4e5f\u8981\u5148\u8dd1\u5b8c finally \u624d\u771f\u6b63\u8fd4\u56de\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b8c\u6574\u7684 try \u7ed3\u6784\u50cf<strong>\u4e00\u6b21\u51fa\u95e8\u65c5\u884c<\/strong>\uff1atry \u662f\u300c\u51fa\u95e8\u901b\u8857\u300d\uff0cexcept \u662f\u300c\u8def\u4e0a\u5835\u8f66\u5c31\u6539\u9053\u300d\uff0celse \u662f\u300c\u987a\u5229\u901b\u5b8c\u4e70\u5230\u4e86\u4e1c\u897f\u300d\uff0cfinally \u662f\u300c\u4e0d\u7ba1\u901b\u6ca1\u901b\u6210\uff0c\u56de\u5bb6\u5fc5\u987b\u9501\u95e8\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u5b8c\u6574\u7ed3\u6784\u2014\u2014\u56db\u6761\u8def\u7684\u6267\u884c\u987a\u5e8f<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    result = 10 \/ 2\nexcept ZeroDivisionError:\n    print(\"\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\")\nelse:\n    print(\"\u8ba1\u7b97\u6210\u529f\uff0c\u7ed3\u679c\u662f\", result)\nfinally:\n    print(\"finally\uff1a\u65e0\u8bba\u6210\u529f\u5931\u8d25\u90fd\u6267\u884c\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8ba1\u7b97\u6210\u529f\uff0c\u7ed3\u679c\u662f 5.0\nfinally\uff1a\u65e0\u8bba\u6210\u529f\u5931\u8d25\u90fd\u6267\u884c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>10 \/ 2<\/code> \u6ca1\u51fa\u9519 \u2192 \u8df3\u8fc7 except \u2192 \u6267\u884c <code>else<\/code> \u6253\u5370\u6210\u529f\u7ed3\u679c \u2192 \u6700\u540e\u6267\u884c <code>finally<\/code>\u3002\u628a\u4ee3\u7801\u6539\u6210 <code>10 \/ 0<\/code> \u518d\u8dd1\uff1a\u8fd9\u6b21\u8d70 except\uff0c\u4f46 finally \u4f9d\u7136\u6267\u884c\u3002<strong>\u4e24\u6761\u8def\u90fd\u901a\u5411 finally<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>finally \u505a\u6e05\u7406\u2014\u2014\u51fa\u9519\u65f6\u6536\u5c3e\u4e5f\u7167\u5e38\u8fdb\u884c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    with open(\"missing.txt\", \"r\") as f:\n        print(f.read())\nexcept FileNotFoundError:\n    print(\"\u6587\u4ef6\u4e0d\u5b58\u5728\")\nfinally:\n    print(\"finally\uff1a\u6e05\u7406\u8d44\u6e90\u7167\u5e38\u8fdb\u884c\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6587\u4ef6\u4e0d\u5b58\u5728\nfinally\uff1a\u6e05\u7406\u8d44\u6e90\u7167\u5e38\u8fdb\u884c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u771f\u5b9e\u573a\u666f\uff1a\u8fd9\u91cc\u6362\u6210\u300c\u6570\u636e\u5e93\u8fde\u63a5\u300d\uff0cfinally \u91cc\u5199\u300c\u5173\u95ed\u8fde\u63a5\u300d\u2014\u2014\u65e0\u8bba\u6210\u529f\u5931\u8d25\uff0c\u8fde\u63a5\u90fd\u88ab\u6b63\u786e\u91ca\u653e\uff0c\u4e0d\u4f1a\u6cc4\u6f0f\u8d44\u6e90\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u8865\u5145\uff1a<code>finally<\/code> \u5148\u4e8e <code>return<\/code> \u6267\u884c<\/strong>\u2014\u2014\u51fd\u6570\u91cc try \u5757\u5373\u4f7f\u5199\u4e86 <code>return<\/code>\uff0c\u8fd4\u56de\u503c\u7b97\u597d\u540e\u4e5f\u8981\u5148\u8dd1\u5b8c <code>finally<\/code> \u624d\u771f\u6b63\u8fd4\u56de\u3002\u6240\u4ee5 <code>finally<\/code> \u53ea\u505a\u6e05\u7406\uff0c**\u4e0d\u8981\u5199 <code>return<\/code>**\uff0c\u5426\u5219\u4f1a\u8986\u76d6 try \u91cc\u7684\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u628a\u6210\u529f\u903b\u8f91\u5199\u5728 try \u91cc \u2192 \u95ee\u9898\uff1a\u5f02\u5e38\u53d1\u751f\u540e\uff0ctry \u91cc\u51fa\u9519\u884c\u4e4b\u540e\u7684\u4ee3\u7801\u5168\u88ab\u8df3\u8fc7\uff0c\u6210\u529f\u903b\u8f91\u4e0d\u6267\u884c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6210\u529f\u903b\u8f91\u653e <code>else<\/code>\uff0c\u53ea\u6709\u300c\u53ef\u80fd\u51fa\u9519\u300d\u7684\u4ee3\u7801\u653e try\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5728 finally \u91cc\u5199 return \u2192 \u95ee\u9898\uff1afinally \u7684 return \u4f1a<strong>\u8986\u76d6<\/strong> try \u91cc\u7684 return\uff0c\u8fd4\u56de\u503c\u88ab\u6084\u6084\u6539\u6389 \u2192 \u6b63\u786e\u5199\u6cd5\uff1afinally \u53ea\u505a\u6e05\u7406\uff0c\u4e0d\u8981\u5199 return\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1atry \u662f\u5c1d\u8bd5\uff0cexcept \u662f\u63a5\u9505\uff1belse \u7b49\u6210\u529f\uff0cfinally \u5fc5\u9501\u95e8\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 raise \u4e0e\u81ea\u5b9a\u4e49\u5f02\u5e38 \u2014\u2014 \u4e3b\u52a8\u8bf4\u4e0d\uff0c\u4e1a\u52a1\u81ea\u5df1\u5b9a\u4e49\u9519\u8bef<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u5f02\u5e38\u90fd\u662f\u7a0b\u5e8f\u300c\u88ab\u52a8\u300d\u89e6\u53d1\u7684\u3002\u4f46\u5f88\u591a\u65f6\u5019<strong>\u4e1a\u52a1\u4e0a\u4e0d\u5141\u8bb8<\/strong>\u7684\u64cd\u4f5c\u9700\u8981\u4f60<strong>\u4e3b\u52a8<\/strong>\u629b\u51fa\u5f02\u5e38\uff1a\u6210\u7ee9\u4e0d\u80fd\u4e3a\u8d1f\u6570\u3001\u5e74\u9f84\u4e0d\u80fd\u8d85 200\u3001\u4f59\u989d\u4e0d\u80fd\u4e3a\u8d1f\u2026\u2026<code>raise<\/code> \u5c31\u662f\u4e3b\u52a8\u629b\u5f02\u5e38\u7684\u8bed\u53e5\uff1b<strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong>\u662f\u7ee7\u627f <code>Exception<\/code> \u521b\u5efa\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\uff0c\u8ba9\u5f02\u5e38\u540d\u76f4\u63a5\u8868\u8fbe\u4e1a\u52a1\u542b\u4e49\uff08\u5982 <code>ScoreTooLowError<\/code>\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>raise \u5f02\u5e38\u5bf9\u8c61<\/code> \u7684\u672c\u8d28\u548c\u89e3\u91ca\u5668\u629b\u5f02\u5e38<strong>\u5b8c\u5168\u4e00\u6837<\/strong>\uff1a\u521b\u5efa\u5f02\u5e38\u5bf9\u8c61\u5e76\u6cbf\u8c03\u7528\u6808\u5411\u4e0a\u4f20\u9012\uff0c\u7b49\u5f85\u4e0a\u5c42 <code>except<\/code> \u63a5\u4f4f\u3002\u6240\u4ee5 <code>raise<\/code> \u548c <code>except<\/code> \u662f\u4e00\u5bf9\u300c\u751f\u4ea7\u8005 \/ \u6d88\u8d39\u8005\u300d\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ea\u9700 <code>class \u540d\u5b57(Exception):<\/code> \u4e00\u884c\uff1a\u7ee7\u627f\u6765\u7684\u6784\u9020\u65b9\u6cd5\u548c <code>__str__<\/code> \u5df2\u80fd\u663e\u793a\u9519\u8bef\u4fe1\u606f\u3002\u6b64\u5916\uff0c<code>raise<\/code> \u8fd8\u6709<strong>\u4e0d\u5e26\u53c2\u6570<\/strong>\u7684\u7528\u6cd5\uff1a\u5728 except \u5757\u91cc\u628a\u521a\u63a5\u4f4f\u7684\u5f02\u5e38\u539f\u6837\u518d\u629b\u7ed9\u4e0a\u5c42\uff0c\u5e38\u7528\u4e8e\u300c\u5148\u8bb0\u5f55\u65e5\u5fd7\uff0c\u518d\u4ea4\u7ed9\u4e0a\u5c42\u5904\u7406\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>raise<\/code> \u50cf<strong>\u6536\u94f6\u53f0\u7684\u62d2\u6536<\/strong>\uff1a\u987e\u5ba2\u62ff\u5047\u5e01\uff08\u975e\u6cd5\u6570\u636e\uff09\u4ed8\u6b3e\uff0c\u6536\u94f6\u5458\u660e\u786e\u8bf4\u300c\u8fd9\u5f20\u4e0d\u884c\u300d\uff08\u629b\u51fa\u5f02\u5e38\uff09\uff0c\u8bf7\u987e\u5ba2\u6362\u4e00\u5f20\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u5219\u662f\u95e8\u5e97\u7684<strong>\u4e13\u5c5e\u89c4\u7ae0<\/strong>\uff1a\u5976\u8336\u5e97\u8bf4\u300c\u7cd6\u5ea6\u8d85\u51fa\u8303\u56f4\u300d\uff0c\u94f6\u884c\u8bf4\u300c\u4f59\u989d\u4e0d\u8db3\u300d\u2014\u2014\u9519\u8bef\u4fe1\u606f\u8d8a\u8d34\u5408\u4e1a\u52a1\uff0c\u5904\u7406\u8d8a\u7cbe\u51c6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>raise \u4e3b\u52a8\u629b\u51fa\u2014\u2014\u628a\u300c\u4e1a\u52a1\u95ee\u9898\u300d\u660e\u786e\u629b\u7ed9\u8c03\u7528\u65b9<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def set_score(score):\n    if score &lt; 0:\n        raise ValueError(f\"\u6210\u7ee9\u4e0d\u80fd\u4e3a\u8d1f\u6570\uff0c\u6536\u5230\uff1a{score}\")\n    return score\n\ntry:\n    set_score(-5)\nexcept ValueError as e:\n    print(\"\u5f55\u5165\u5931\u8d25\uff1a\", e)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5f55\u5165\u5931\u8d25\uff1a \u6210\u7ee9\u4e0d\u80fd\u4e3a\u8d1f\u6570\uff0c\u6536\u5230\uff1a-5\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>set_score(-5)<\/code> \u53d1\u73b0\u6210\u7ee9\u4e3a\u8d1f\uff0c<code>raise ValueError(...)<\/code> \u4e3b\u52a8\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff08\u6d88\u606f\u91cc\u5e26\u4e0a\u73b0\u573a\u6570\u636e -5\uff09\uff1b\u8c03\u7528\u65b9\u7528 <code>except ValueError<\/code> \u63a5\u4f4f\u5e76\u63d0\u793a\u3002<strong>\u6ce8\u610f\uff1a<code>raise<\/code> \u4e4b\u540e\u7684 <code>return score<\/code> \u4e0d\u4f1a\u6267\u884c<\/strong>\u2014\u2014\u5f02\u5e38\u8ba9\u51fd\u6570\u7acb\u5373\u4e2d\u65ad\uff0c\u63a7\u5236\u6743\u4ea4\u7ed9\u8c03\u7528\u65b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u81ea\u5b9a\u4e49\u5f02\u5e38\u2014\u2014\u8ba9\u5f02\u5e38\u540d\u8868\u8fbe\u4e1a\u52a1\u542b\u4e49<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ScoreTooLowError(Exception):\n    \"\"\"\u6210\u7ee9\u8fc7\u4f4e\u5f02\u5e38\"\"\"\n\ndef check_pass(score):\n    if score &lt; 60:\n        raise ScoreTooLowError(f\"\u6210\u7ee9 {score} \u4e0d\u53ca\u683c\")\n    print(\"\u6210\u7ee9\u5408\u683c\")\n\ntry:\n    check_pass(55)\nexcept ScoreTooLowError as e:\n    print(\"\u53d1\u73b0\u5f02\u5e38\uff1a\", e)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u53d1\u73b0\u5f02\u5e38\uff1a \u6210\u7ee9 55 \u4e0d\u53ca\u683c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>class ScoreTooLowError(Exception):<\/code> \u4e00\u884c\u5b9a\u4e49\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1b<code>except ScoreTooLowError<\/code> \u7cbe\u786e\u63a5\u4f4f\u8fd9\u4e00\u7c7b\u4e1a\u52a1\u95ee\u9898\u3002<strong>\u81ea\u5b9a\u4e49\u5f02\u5e38\u8ba9\u4ee3\u7801\u8bfb\u8d77\u6765\u50cf\u4e1a\u52a1\u8bf4\u660e\u4e66<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u8865\u5145\uff1a\u65e0\u53c2\u6570 <code>raise<\/code><strong>\u2014\u2014\u5728 <code>except<\/code> \u5757\u91cc\u5199 <code>raise<\/code>\uff08\u4e0d\u5e26\u53c2\u6570\uff09\uff0c\u628a\u521a\u63a5\u4f4f\u7684<\/strong>\u540c\u4e00\u4e2a<\/strong>\u5f02\u5e38\u5bf9\u8c61\u539f\u6837\u629b\u7ed9\u4e0a\u5c42\u3002\u771f\u5b9e\u9879\u76ee\u5e38\u7528\u300c\u5c42\u5c42\u4e0a\u62a5\u300d\uff1a\u672c\u5c42\u5148\u8bb0\u5f55\u65e5\u5fd7\uff0c\u518d\u65e0\u53c2\u6570 raise \u4ea4\u7ed9\u4e0a\u5c42\u5904\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>raise ValueError<\/code>\uff08\u4e0d\u5e26\u62ec\u53f7\uff09 \u2192 \u95ee\u9898\uff1a\u629b\u7684\u662f<strong>\u5f02\u5e38\u7c7b\u672c\u8eab<\/strong>\u800c\u4e0d\u662f\u5f02\u5e38\u5bf9\u8c61\uff0c\u62a5\u9519\u4fe1\u606f\u663e\u793a\u7c7b\u800c\u4e0d\u662f\u63cf\u8ff0 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>raise ValueError(\"\u63cf\u8ff0\u4fe1\u606f\")<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u5fd8\u4e86\u7ee7\u627f <code>Exception<\/code> \u2192 \u95ee\u9898\uff1a\u5f02\u5e38\u53d8\u6210\u666e\u901a\u7c7b\uff0c<code>except<\/code> \u63a5\u4e0d\u4f4f\u5b83 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5fc5\u987b\u5199 <code>class \u540d\u5b57(Exception):<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5728 except \u91cc\u60f3\u300c\u8bb0\u5f55\u540e\u7ee7\u7eed\u629b\u300d\u5374\u5199\u4e86 <code>raise NewError(...)<\/code> \u2192 \u95ee\u9898\uff1a\u6362\u4e86\u65b0\u5f02\u5e38\uff0c\u539f\u6765\u7684\u5f02\u5e38\u4fe1\u606f\u4e22\u5931 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u53ea\u60f3\u539f\u6837\u4e0a\u629b\u5c31\u7528\u65e0\u53c2\u6570 <code>raise<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1araise \u4e3b\u52a8\u8bf4\u4e0d\uff0c\u4e1a\u52a1\u5f02\u5e38\u81ea\u5b9a\u4e49\uff1b\u7c7b\u540d\u7ee7\u627f Exception\uff0c\u65e0\u53c2 raise \u539f\u6837\u629b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 assert \u65ad\u8a00 \u2014\u2014 \u5feb\u901f\u81ea\u6211\u68c0\u67e5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>assert<\/code> \u65ad\u8a00\u7528\u4e8e\u68c0\u67e5\u300c\u8fd9\u91cc\u7406\u5e94\u6210\u7acb\u7684\u6761\u4ef6\u300d\uff0c\u6761\u4ef6\u4e0d\u6210\u7acb\u5c31\u629b <code>AssertionError<\/code>\u3002\u5b83\u9002\u5408\u505a\u7a0b\u5e8f\u5185\u90e8\u7684\u524d\u7f6e\u68c0\u67e5\uff0c\u6bd4\u5982\u51fd\u6570\u5165\u53c2\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u57fa\u672c\u7528\u6cd5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def enroll(name, age):\n    assert age &gt;= 6, \"\u5165\u5b66\u5e74\u9f84\u5fc5\u987b\u5927\u4e8e\u7b49\u4e8e 6 \u5c81\"\n    print(f\"{name} \u62a5\u540d\u6210\u529f\")\n\nenroll(\"\u5c0f\u660e\", 7)     # \u6b63\u5e38\n# enroll(\"\u5c0f\u5f3a\", 4)   # \u53d6\u6d88\u6ce8\u91ca\u4f1a\u629b AssertionError\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5c0f\u660e \u62a5\u540d\u6210\u529f\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u65ad\u8a00\u4e0e <code>raise<\/code> \u7684\u533a\u522b<\/strong>\uff1a<code>raise<\/code> \u5904\u7406\u300c\u7528\u6237\u8f93\u5165\u7b49\u5916\u90e8\u9519\u8bef\u300d\uff0c\u662f\u7a0b\u5e8f\u6b63\u5f0f\u529f\u80fd\u7684\u4e00\u90e8\u5206\uff1b<code>assert<\/code> \u662f\u5f00\u53d1\u671f\u300c\u5185\u90e8\u5047\u8bbe\u300d\u7684\u68c0\u67e5\uff0c\u7528\u4e8e\u5feb\u901f\u66b4\u9732 bug\uff0c\u901a\u5e38\u4e0d\u7528\u4e8e\u5904\u7406\u7528\u6237\u8f93\u5165\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7528 <code>assert<\/code> \u5904\u7406\u7528\u6237\u8f93\u5165 \u2192 \u95ee\u9898\uff1a\u65ad\u8a00\u662f\u5f00\u53d1\u671f\u68c0\u67e5\uff0c\u751f\u4ea7\u73af\u5883\u53ef\u80fd\u88ab\u5173\u95ed\uff0c\u5904\u7406\u7528\u6237\u8f93\u5165\u8981\u7528 try\/except + raise \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5916\u90e8\u8f93\u5165\u9519\u8bef\u7528 <code>raise<\/code>\uff0c\u5185\u90e8\u5047\u8bbe\u7528 <code>assert<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1aassert \u67e5\u5047\u8bbe\uff0c\u4e0d\u6210\u7acb\u5c31\u629b\uff1b\u5185\u90e8\u68c0\u67e5\u7528\u5b83\uff0c\u7528\u6237\u8f93\u5165\u9760 raise\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6 \u52a8\u624b\u5b9e\u8df5\uff1a\u5065\u58ee\u7248\u8ba1\u7b97\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u505a\u4e00\u4e2a\u56db\u5219\u8fd0\u7b97\u8ba1\u7b97\u5668\uff0c\u4efb\u610f\u8f93\u5165\u90fd\u4e0d\u5d29\u6e83\uff1a\u9664\u96f6\u3001\u8f93\u5165\u975e\u6570\u5b57\u3001\u8fd0\u7b97\u7b26\u4e0d\u8ba4\u8bc6\uff0c\u90fd\u7ed9\u51fa\u53cb\u597d\u63d0\u793a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># robust_calculator.py \u2014\u2014 \u5065\u58ee\u7248\u8ba1\u7b97\u5668\nclass CalcError(Exception):\n    \"\"\"\u8ba1\u7b97\u5668\u4e1a\u52a1\u5f02\u5e38\"\"\"\n\ndef calculate(a, b, op):\n    if op not in (\"+\", \"-\", \"*\", \"\/\"):\n        raise CalcError(f\"\u4e0d\u652f\u6301\u7684\u8fd0\u7b97\u7b26\uff1a{op}\")\n    if op == \"\/\" and b == 0:\n        raise CalcError(\"\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\")\n    if op == \"+\":\n        return a + b\n    if op == \"-\":\n        return a - b\n    if op == \"*\":\n        return a * b\n    return a \/ b\n\ndef main():\n    while True:\n        expr = input(\"\u8f93\u5165\u7b97\u5f0f\uff08\u5982 3 + 5\uff0cquit \u9000\u51fa\uff09\uff1a\")\n        if expr == \"quit\":\n            break\n        parts = expr.split()\n        if len(parts) != 3:\n            print(\"\u683c\u5f0f\u9519\u8bef\uff0c\u793a\u4f8b\uff1a3 + 5\")\n            continue\n        a_str, op, b_str = parts\n        try:\n            a = float(a_str)\n            b = float(b_str)\n            result = calculate(a, b, op)\n            print(f\"{a} {op} {b} = {result}\")\n        except ValueError:\n            print(\"\u6570\u5b57\u683c\u5f0f\u4e0d\u5bf9\uff0c\u8bf7\u8f93\u5165\u5408\u6cd5\u6570\u5b57\")\n        except CalcError as e:\n            print(\"\u8ba1\u7b97\u5931\u8d25\uff1a\", e)\n        except Exception as e:\n            print(\"\u672a\u77e5\u9519\u8bef\uff1a\", e)\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u6548\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8f93\u5165\u7b97\u5f0f\uff08\u5982 3 + 5\uff0cquit \u9000\u51fa\uff09\uff1a10 \/ 0\n\u8ba1\u7b97\u5931\u8d25\uff1a \u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\n\u8f93\u5165\u7b97\u5f0f\uff08\u5982 3 + 5\uff0cquit \u9000\u51fa\uff09\uff1aabc + 2\n\u6570\u5b57\u683c\u5f0f\u4e0d\u5bf9\uff0c\u8bf7\u8f93\u5165\u5408\u6cd5\u6570\u5b57\n\u8f93\u5165\u7b97\u5f0f\uff08\u5982 3 + 5\uff0cquit \u9000\u51fa\uff09\uff1a5 % 2\n\u8ba1\u7b97\u5931\u8d25\uff1a \u4e0d\u652f\u6301\u7684\u8fd0\u7b97\u7b26\uff1a%\n\u8f93\u5165\u7b97\u5f0f\uff08\u5982 3 + 5\uff0cquit \u9000\u51fa\uff09\uff1a3 + 5\n3.0 + 5.0 = 8.0\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\u7a0b\u5e8f\u7684\u300c\u5206\u5c42\u300d\uff1a<code>calculate<\/code> \u8d1f\u8d23\u53d1\u73b0\u4e1a\u52a1\u95ee\u9898\u5e76\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1b<code>main<\/code> \u8d1f\u8d23\u6355\u83b7\u5e76\u63d0\u793a\u7528\u6237\u3002\u8fd9\u6837\u804c\u8d23\u6e05\u6670\uff0c\u540e\u7eed\u7ed9\u8ba1\u7b97\u5668\u52a0\u529f\u80fd\u4e5f\u4e0d\u7528\u6539\u9519\u8bef\u5904\u7406\u903b\u8f91\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5347\u7ea7\u6311\u6218<\/strong>\uff1a\u7ed9\u8ba1\u7b97\u5668\u52a0\u4e00\u4e2a\u300c\u8fde\u7eed\u8fd0\u7b97\u300d\u529f\u80fd\uff0c\u628a\u4e0a\u4e00\u6b21\u7ed3\u679c\u5b58\u4e0b\u6765\uff0c\u7528 <code>ans<\/code> \u8868\u793a\uff08\u6bd4\u5982\u8f93\u5165 <code>ans + 10<\/code>\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4eca\u65e5\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f02\u5e38\u662f\u5bf9\u8c61\uff1a\u6709\u7c7b\u578b\u3001\u6709\u63cf\u8ff0\u3001\u6709\u7ee7\u627f\u94fe\uff08\u5177\u4f53\u5f02\u5e38 \u2192 Exception \u2192 BaseException\uff09\nTraceback \u4e09\u6bb5\u4fe1\u606f\uff1a\u51fa\u9519\u4f4d\u7f6e \u2192 \u5f02\u5e38\u7c7b\u578b \u2192 \u5177\u4f53\u539f\u56e0\uff0c\u62a5\u9519\u4e0d\u53ef\u6015\n<code>try<\/code> \/ <code>except<\/code> \u628a\u300c\u53ef\u80fd\u51fa\u9519\u300d\u548c\u300c\u51fa\u9519\u600e\u4e48\u529e\u300d\u5206\u5f00\uff0cexcept \u540e\u7a0b\u5e8f\u7ee7\u7eed\u8fd0\u884c\n\u591a\u4e2a except \u4ece\u4e0a\u5230\u4e0b\u5339\u914d\uff1a\u5177\u4f53\u7684\u5199\u524d\u9762\uff0c<code>except Exception<\/code> \u515c\u5e95\u5199\u6700\u540e\n<code>else<\/code> \u53ea\u5728\u65e0\u5f02\u5e38\u65f6\u6267\u884c\uff08\u6210\u529f\u5206\u652f\uff09\uff0c<code>finally<\/code> \u65e0\u8bba\u6210\u8d25\u90fd\u6267\u884c\uff08\u8d44\u6e90\u6e05\u7406\uff09\uff0c\u4e14 finally \u5148\u4e8e return\n<code>raise<\/code> \u4e3b\u52a8\u629b\u5f02\u5e38\uff0c\u65e0\u53c2\u6570 raise \u628a\u539f\u5f02\u5e38\u539f\u6837\u4e0a\u629b\uff08\u5148\u8bb0\u5f55\u518d\u4e0a\u62a5\uff09\n\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a<code>class \u540d\u5b57(Exception):<\/code> \u4e00\u884c\u5b9a\u4e49\uff0c\u8ba9\u5f02\u5e38\u540d\u8868\u8fbe\u4e1a\u52a1\u542b\u4e49\n<code>assert<\/code> \u65ad\u8a00\u7528\u4e8e\u5f00\u53d1\u671f\u5185\u90e8\u5047\u8bbe\u68c0\u67e5\uff0c\u4e0d\u7528\u4e8e\u5904\u7406\u7528\u6237\u8f93\u5165\n\u5065\u58ee\u7248\u8ba1\u7b97\u5668\u9762\u5bf9\u9664\u96f6\u3001\u4e71\u8f93\u5165\u3001\u672a\u77e5\u8fd0\u7b97\u7b26\u90fd\u4e0d\u5d29\u6e83<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u62a5\u9519<\/th><th>\u539f\u56e0<\/th><th>\u4fee\u590d<\/th><\/tr><\/thead><tbody><tr><td><code>ZeroDivisionError<\/code><\/td><td>\u9664\u6570\u4e3a 0<\/td><td>\u8fd0\u7b97\u524d\u5224\u65ad\uff0c\u6216\u6355\u83b7\u540e\u63d0\u793a<\/td><\/tr><tr><td><code>ValueError: invalid literal for int()<\/code><\/td><td>\u628a\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6210\u6570\u5b57<\/td><td><code>try<\/code> \u6355\u83b7\uff0c\u63d0\u793a\u91cd\u65b0\u8f93\u5165<\/td><\/tr><tr><td><code>IndexError<\/code><\/td><td>\u8bbf\u95ee\u5217\u8868\u4e0d\u5b58\u5728\u7684\u4e0b\u6807\uff08\u5982 <code>split<\/code> \u540e\u53d6\u7b2c 4 \u4e2a\u5143\u7d20\uff09<\/td><td>\u5148\u68c0\u67e5 <code>len()<\/code>\uff0c\u518d\u53d6\u4e0b\u6807<\/td><\/tr><tr><td><code>AssertionError<\/code><\/td><td><code>assert<\/code> \u6761\u4ef6\u4e0d\u6210\u7acb<\/td><td>\u68c0\u67e5\u4f20\u5165\u6570\u636e\uff0c\u4fee\u6b63\u903b\u8f91<\/td><\/tr><tr><td><code>except<\/code> \u6ca1\u751f\u6548\uff0c\u5f02\u5e38\u7ee7\u7eed\u629b\u51fa<\/td><td>\u6355\u83b7\u7684\u7c7b\u578b\u4e0d\u5bf9\uff08\u5982\u4ee3\u7801\u629b <code>TypeError<\/code> \u5374\u6355\u83b7 <code>ValueError<\/code>\uff09<\/td><td>\u5148\u7528 <code>except Exception as e<\/code> \u6253\u5370\u771f\u5b9e\u7c7b\u578b\uff0c\u518d\u6539<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 \u5f02\u5e38\u662f\u4ec0\u4e48 \u2014\u2014 \u7a0b\u5e8f\u51fa\u9519\u65f6\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48 \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f \u5f02\u5e38\uff08exception\uff09\uff1a\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53d1\u751f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[16,9],"tags":[17],"class_list":["post-92","post","type-post","status-publish","format-standard","hentry","category-python","category-code-dev","tag-python-day"],"_links":{"self":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"predecessor-version":[{"id":142,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/92\/revisions\/142"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}