{"id":990767,"date":"2024-12-27T08:22:53","date_gmt":"2024-12-27T00:22:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/990767.html"},"modified":"2024-12-27T08:22:55","modified_gmt":"2024-12-27T00:22:55","slug":"python%e5%a6%82%e4%bd%95%e8%b7%b3%e8%bd%ac%e9%94%99%e8%af%af%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/990767.html","title":{"rendered":"Python\u5982\u4f55\u8df3\u8f6c\u9519\u8bef\u8bed\u53e5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065343\/5423e7d1-7199-42ea-b4bd-fa645f592be7.webp\" alt=\"Python\u5982\u4f55\u8df3\u8f6c\u9519\u8bef\u8bed\u53e5\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8df3\u8f6c\u9519\u8bef\u8bed\u53e5\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528try-except\u8bed\u53e5\u3001\u7ed3\u5408finally\u5b50\u53e5\u3001\u5229\u7528else\u5b50\u53e5\u3002<\/strong>\u5176\u4e2d\uff0c<strong>try-except\u8bed\u53e5<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u5f02\u5e38\uff0c\u4ece\u800c\u9632\u6b62\u7a0b\u5e8f\u56e0\u9519\u8bef\u800c\u4e2d\u65ad\u3002\u901a\u8fc7try-except\u8bed\u53e5\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u5728\u9047\u5230\u9519\u8bef\u65f6\u7ee7\u7eed\u6267\u884c\u4e0b\u53bb\uff0c\u800c\u4e0d\u662f\u7ec8\u6b62\u8fd0\u884c\u3002\u4ee5\u4e0b\u662f\u5bf9<strong>try-except\u8bed\u53e5<\/strong>\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><p><strong>try-except\u8bed\u53e5<\/strong>\uff1atry-except\u8bed\u53e5\u662fPython\u4e2d\u5904\u7406\u5f02\u5e38\u7684\u57fa\u672c\u7ed3\u6784\u3002\u5b83\u5141\u8bb8\u7a0b\u5e8f\u6355\u83b7\u5e76\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u800c\u4e0d\u81f3\u4e8e\u7a0b\u5e8f\u5d29\u6e83\u3002\u5728try\u5757\u4e2d\u653e\u7f6e\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u5f53\u5f02\u5e38\u53d1\u751f\u65f6\uff0c\u63a7\u5236\u6d41\u4f1a\u8df3\u8f6c\u5230\u5bf9\u5e94\u7684except\u5757\u8fdb\u884c\u5904\u7406\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u5728\u9047\u5230\u9519\u8bef\u65f6\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4ee3\u7801\uff0c\u800c\u4e0d\u81f3\u4e8e\u5b8c\u5168\u4e2d\u65ad\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001TRY-EXCEPT\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0ctry-except\u7ed3\u6784\u662f\u5904\u7406\u5f02\u5e38\u7684\u57fa\u7840\u3002\u5f02\u5e38\u901a\u5e38\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u51fa\u73b0\u7684\u9519\u8bef\uff0c\u901a\u8fc7\u6355\u83b7\u8fd9\u4e9b\u9519\u8bef\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u907f\u514d\u5d29\u6e83\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>try-except\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>except SomeException as e:<\/p>\n<p>    # \u5f02\u5e38\u5904\u7406\u4ee3\u7801<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\uff0ctry\u5757\u4e2d\u7684\u4ee3\u7801\u4f1a\u5c1d\u8bd5\u6267\u884c\uff0c\u5982\u679c\u6ca1\u6709\u5f02\u5e38\u53d1\u751f\uff0c\u7a0b\u5e8f\u5c06\u7ee7\u7eed\u6267\u884ctry\u5757\u4e4b\u540e\u7684\u4ee3\u7801\u3002\u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0c\u7a0b\u5e8f\u5c06\u8df3\u8f6c\u5230except\u5757\uff0c\u5e76\u6267\u884c\u5176\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u591a\u91cd\u5f02\u5e38\u6355\u83b7<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0ctry\u5757\u4e2d\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u5f15\u53d1\u591a\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2atry\u8bed\u53e5\u4e2d\u4f7f\u7528\u591a\u4e2aexcept\u5b50\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u5f15\u53d1\u591a\u79cd\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>except ValueError:<\/p>\n<p>    # \u5904\u7406ValueError\u7684\u4ee3\u7801<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    # \u5904\u7406ZeroDivisionError\u7684\u4ee3\u7801<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7ed3\u6784\u5141\u8bb8\u7a0b\u5e8f\u6839\u636e\u4e0d\u540c\u7684\u5f02\u5e38\u7c7b\u578b\u63d0\u4f9b\u4e0d\u540c\u7684\u5904\u7406\u65b9\u6848\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001FINALLY\u5b50\u53e5<\/p>\n<\/p>\n<p><p>finally\u5b50\u53e5\u7528\u4e8e\u5728try-except\u7ed3\u6784\u4e2d\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0cfinally\u5757\u4e2d\u7684\u4ee3\u7801\u90fd\u4f1a\u6267\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>except SomeException as e:<\/p>\n<p>    # \u5f02\u5e38\u5904\u7406\u4ee3\u7801<\/p>\n<p>finally:<\/p>\n<p>    # \u603b\u662f\u6267\u884c\u7684\u4ee3\u7801<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>finally\u5757\u4e2d\u7684\u4ee3\u7801\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u6267\u884c\uff0c\u901a\u5e38\u7528\u4e8e\u91ca\u653e\u8d44\u6e90\u6216\u6267\u884c\u5176\u4ed6\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<p><p>finally\u5b50\u53e5\u5728\u9700\u8981\u4fdd\u8bc1\u67d0\u4e9b\u64cd\u4f5c\u59cb\u7ec8\u6267\u884c\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u9700\u8981\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>try:<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>except IOError:<\/p>\n<p>    # \u5904\u7406IO\u9519\u8bef<\/p>\n<p>finally:<\/p>\n<p>    file.close()  # \u786e\u4fdd\u6587\u4ef6\u59cb\u7ec8\u5173\u95ed<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001ELSE\u5b50\u53e5<\/p>\n<\/p>\n<p><p>else\u5b50\u53e5\u53ef\u4ee5\u4e0etry-except\u7ed3\u6784\u7ed3\u5408\u4f7f\u7528\u3002\u5b83\u4f1a\u5728try\u5757\u6ca1\u6709\u5f15\u53d1\u4efb\u4f55\u5f02\u5e38\u65f6\u6267\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>except SomeException as e:<\/p>\n<p>    # \u5f02\u5e38\u5904\u7406\u4ee3\u7801<\/p>\n<p>else:<\/p>\n<p>    # \u6ca1\u6709\u5f02\u5e38\u65f6\u6267\u884c\u7684\u4ee3\u7801<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>else\u5b50\u53e5\u7528\u4e8e\u5728try\u5757\u6210\u529f\u6267\u884c\u540e\u6267\u884c\u4e00\u4e9b\u989d\u5916\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<p><p>else\u5b50\u53e5\u901a\u5e38\u7528\u4e8e\u5728try\u5757\u6210\u529f\u6267\u884c\u540e\u6267\u884c\u4e00\u4e9b\u540e\u7eed\u64cd\u4f5c\uff0c\u800c\u8fd9\u4e9b\u64cd\u4f5c\u4e0d\u5e94\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = divide(10, 2)<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    print(&quot;Cannot divide by zero!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Division successful! Result is:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u6355\u83b7\u6240\u6709\u5f02\u5e38<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u6355\u83b7\u6240\u6709\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528except\u540e\u4e0d\u5e26\u5177\u4f53\u5f02\u5e38\u7c7b\u578b\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>except:<\/p>\n<p>    # \u5904\u7406\u6240\u6709\u5f02\u5e38<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5c06\u6355\u83b7\u6240\u6709\u672a\u88ab\u524d\u9762except\u5b50\u53e5\u6355\u83b7\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136\u6355\u83b7\u6240\u6709\u5f02\u5e38\u53ef\u4ee5\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\uff0c\u4f46\u4e5f\u53ef\u80fd\u63a9\u76d6\u6f5c\u5728\u7684\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u65f6\u5e94\u8c28\u614e\uff0c\u901a\u5e38\u5e94\u8bb0\u5f55\u4e0b\u5f02\u5e38\u4fe1\u606f\u4ee5\u4fbf\u540e\u7eed\u6392\u67e5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5f02\u5e38\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u5728except\u5b50\u53e5\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5bf9\u8c61\u6765\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6355\u83b7\u5f02\u5e38\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>except SomeException as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8bbf\u95ee\u5f02\u5e38\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f8b\u5982\u9519\u8bef\u6d88\u606f\u6216\u5806\u6808\u8ddf\u8e2a\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/p>\n<\/p>\n<p><p>Python\u5141\u8bb8\u4f60\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\uff0c\u4ee5\u4fbf\u5728\u7a0b\u5e8f\u4e2d\u629b\u51fa\u548c\u6355\u83b7\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyCustomError(Exception):<\/p>\n<p>    pass<\/p>\n<p>try:<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se MyCustomError(&quot;This is a custom error&quot;)<\/p>\n<p>except MyCustomError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u4e0e\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u786e\u4fdd\u7a0b\u5e8f\u5065\u58ee\u6027\u7684\u91cd\u8981\u624b\u6bb5\u3002\u5408\u7406\u4f7f\u7528try-except\u7ed3\u6784\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5bb9\u9519\u80fd\u529b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5e38\u89c1\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u6587\u4ef6\u64cd\u4f5c<\/strong>\uff1a\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u3001\u5199\u5165\u65f6\u53ef\u80fd\u51fa\u73b0\u7684IOError\u3002<\/li>\n<li><strong>\u7f51\u7edc\u8bf7\u6c42<\/strong>\uff1a\u6355\u83b7\u7f51\u7edc\u8bf7\u6c42\u4e2d\u7684\u8fde\u63a5\u9519\u8bef\u3001\u8d85\u65f6\u7b49\u5f02\u5e38\u3002<\/li>\n<li><strong>\u7528\u6237\u8f93\u5165<\/strong>\uff1a\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u662f\u5426\u7b26\u5408\u9884\u671f\uff0c\u6355\u83b7ValueError\u7b49\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u5efa\u8bae<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7cbe\u51c6\u6355\u83b7<\/strong>\uff1a\u5c3d\u91cf\u6355\u83b7\u5177\u4f53\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u901a\u7528except\u3002<\/li>\n<li><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong>\uff1a\u5728\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u4e2d\u8bb0\u5f55\u65e5\u5fd7\uff0c\u4ee5\u4fbf\u540e\u7eed\u6392\u67e5\u95ee\u9898\u3002<\/li>\n<li><strong>\u4fdd\u6301\u7b80\u6d01<\/strong>\uff1a\u907f\u514d\u5728try\u5757\u4e2d\u653e\u7f6e\u8fc7\u591a\u4ee3\u7801\uff0c\u5c3d\u91cf\u5c06\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\u96c6\u4e2d\u5728\u4e00\u8d77\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528try-except\u7ed3\u6784\u548c\u5176\u4ed6\u5f02\u5e38\u5904\u7406\u6280\u672f\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8Python\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u548c\u8df3\u8fc7\u9519\u8bef\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u9519\u8bef\u3002\u5f53\u4ee3\u7801\u5757\u4e2d\u7684\u8bed\u53e5\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u4ece\u800c\u5b9e\u73b0\u8df3\u8fc7\u9519\u8bef\u7684\u76ee\u7684\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    # \u53ef\u80fd\u4ea7\u751f\u9519\u8bef\u7684\u4ee3\u7801\n    result = 10 \/ 0\nexcept ZeroDivisionError:\n    print(&quot;\u51fa\u73b0\u4e86\u9664\u4ee5\u96f6\u7684\u9519\u8bef\uff0c\u6b63\u5728\u8df3\u8fc7\u6b64\u9519\u8bef\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7a0b\u5e8f\u80fd\u591f\u7ee7\u7eed\u8fd0\u884c\uff0c\u800c\u4e0d\u4f1a\u56e0\u4e3a\u9519\u8bef\u800c\u505c\u6b62\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u4ee5\u4fbf\u540e\u7eed\u8c03\u8bd5\uff1f<\/strong><br \/>\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u3002\u5728<code>except<\/code>\u5757\u4e2d\uff0c\u53ef\u4ee5\u5c06\u5f02\u5e38\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u8c03\u8bd5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import logging\n\nlogging.basicConfig(filename=&#39;error.log&#39;, level=logging.ERROR)\n\ntry:\n    # \u53ef\u80fd\u4ea7\u751f\u9519\u8bef\u7684\u4ee3\u7801\n    result = 10 \/ 0\nexcept Exception as e:\n    logging.error(&quot;\u53d1\u751f\u9519\u8bef: %s&quot;, e)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u6240\u6709\u9519\u8bef\u4fe1\u606f\u90fd\u4f1a\u88ab\u8bb0\u5f55\u5230\u540d\u4e3a<code>error.log<\/code>\u7684\u6587\u4ef6\u4e2d\uff0c\u65b9\u4fbf\u540e\u671f\u67e5\u770b\u548c\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684<code>else<\/code>\u8bed\u53e5\u6765\u5904\u7406\u6ca1\u6709\u9519\u8bef\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728<code>try<\/code>\u8bed\u53e5\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>else<\/code>\u8bed\u53e5\u6765\u5904\u7406\u6ca1\u6709\u53d1\u751f\u5f02\u5e38\u7684\u60c5\u51b5\u3002\u5f53<code>try<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u6210\u529f\u6267\u884c\u800c\u6ca1\u6709\u629b\u51fa\u4efb\u4f55\u5f02\u5e38\u65f6\uff0c<code>else<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u5c06\u88ab\u6267\u884c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    result = 10 \/ 2\nexcept ZeroDivisionError:\n    print(&quot;\u51fa\u73b0\u4e86\u9664\u4ee5\u96f6\u7684\u9519\u8bef\u3002&quot;)\nelse:\n    print(&quot;\u8ba1\u7b97\u6210\u529f\uff0c\u7ed3\u679c\u662f:&quot;, result)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u7ed3\u6784\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5c06\u9519\u8bef\u5904\u7406\u548c\u6b63\u5e38\u60c5\u51b5\u7684\u5904\u7406\u903b\u8f91\u5206\u5f00\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u6613\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8df3\u8f6c\u9519\u8bef\u8bed\u53e5\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528try-except\u8bed\u53e5\u3001\u7ed3\u5408finally\u5b50\u53e5\u3001\u5229\u7528else\u5b50\u53e5 [&hellip;]","protected":false},"author":3,"featured_media":990773,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990767"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=990767"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990767\/revisions"}],"predecessor-version":[{"id":990777,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990767\/revisions\/990777"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/990773"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=990767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=990767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=990767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}