{"id":1051256,"date":"2024-12-31T14:14:50","date_gmt":"2024-12-31T06:14:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1051256.html"},"modified":"2024-12-31T14:14:53","modified_gmt":"2024-12-31T06:14:53","slug":"python%e8%b0%83%e8%af%95%e7%9a%84%e6%97%b6%e5%80%99%e5%a6%82%e4%bd%95%e8%b7%b3%e8%bf%87%e8%bf%99%e4%b8%aa%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1051256.html","title":{"rendered":"python\u8c03\u8bd5\u7684\u65f6\u5019\u5982\u4f55\u8df3\u8fc7\u8fd9\u4e2a\u9519\u8bef"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/88744606-232f-4c2e-8ff1-93dd721efc61.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u8c03\u8bd5\u7684\u65f6\u5019\u5982\u4f55\u8df3\u8fc7\u8fd9\u4e2a\u9519\u8bef\" \/><\/p>\n<p><p> <strong>\u5728Python\u8c03\u8bd5\u65f6\u8df3\u8fc7\u9519\u8bef\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528try-except\u4ee3\u7801\u5757\u3001\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u5de5\u5177\u3001\u4fee\u6539\u4ee3\u7801\u7ed3\u6784\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528try-except\u4ee3\u7801\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7try-except\u4ee3\u7801\u5757\uff0c\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u56e0\u9519\u8bef\u800c\u4e2d\u65ad\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4ea7\u751f\u9519\u8bef\u7684\u4ee3\u7801<\/p>\n<p>except SomeException as e:<\/p>\n<p>    # \u5904\u7406\u9519\u8bef<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5728\u9519\u8bef\u53d1\u751f\u65f6\u6355\u83b7\u5f02\u5e38\u5e76\u7ee7\u7eed\u6267\u884c\u7a0b\u5e8f\uff0c\u800c\u4e0d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u8c03\u8bd5\u548c\u8df3\u8fc7\u9519\u8bef\u7684\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528try-except\u4ee3\u7801\u5757<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528try-except\u4ee3\u7801\u5757\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u8bef\u5904\u7406\u65b9\u6cd5\u3002\u5b83\u80fd\u591f\u6355\u83b7\u5e76\u5904\u7406\u8fd0\u884c\u65f6\u7684\u9519\u8bef\uff0c\u800c\u4e0d\u4f1a\u8ba9\u7a0b\u5e8f\u5d29\u6e83\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u786e\u5b9a\u53ef\u80fd\u4ea7\u751f\u9519\u8bef\u7684\u4ee3\u7801\u6bb5<\/strong>\uff1a\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u8bc6\u522b\u51fa\u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\u6bb5\u3002<\/li>\n<li><strong>\u4f7f\u7528try-except\u4ee3\u7801\u5757\u5305\u88f9\u8be5\u4ee3\u7801\u6bb5<\/strong>\uff1a\u5c06\u8bc6\u522b\u51fa\u7684\u4ee3\u7801\u6bb5\u653e\u5165try\u5757\u4e2d\uff0c\u5e76\u5728except\u5757\u4e2d\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 0  # \u53ef\u80fd\u4ea7\u751fZeroDivisionError<\/p>\n<p>except ZeroDivisionError as e:<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)  # \u5904\u7406\u9519\u8bef<\/p>\n<p>    result = None  # \u8bbe\u7f6e\u9ed8\u8ba4\u503c\u6216\u6267\u884c\u5176\u4ed6\u64cd\u4f5c<\/p>\n<p>print(result)  # \u8f93\u51faNone<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u786e\u4fdd\u5373\u4f7f\u53d1\u751f\u4e86\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4e5f\u80fd\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u5de5\u5177\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u8df3\u8fc7\u6216\u67e5\u770b\u9519\u8bef\u7684\u5177\u4f53\u539f\u56e0\u3002\u5927\u591a\u6570\u73b0\u4ee3\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001VSCode\u7b49\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8c03\u8bd5\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u5de5\u5177\u7684\u4e00\u822c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbe\u7f6e\u65ad\u70b9<\/strong>\uff1a\u5728\u53ef\u80fd\u53d1\u751f\u9519\u8bef\u7684\u4ee3\u7801\u884c\u8bbe\u7f6e\u65ad\u70b9\u3002<\/li>\n<li><strong>\u8fd0\u884c\u8c03\u8bd5\u6a21\u5f0f<\/strong>\uff1a\u542f\u52a8\u8c03\u8bd5\u6a21\u5f0f\uff0c\u7a0b\u5e8f\u4f1a\u5728\u8bbe\u7f6e\u7684\u65ad\u70b9\u5904\u6682\u505c\u3002<\/li>\n<li><strong>\u9010\u884c\u8c03\u8bd5<\/strong>\uff1a\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u9010\u884c\u6267\u884c\u4ee3\u7801\uff0c\u89c2\u5bdf\u53d8\u91cf\u7684\u503c\u548c\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\u3002<\/li>\n<li><strong>\u8df3\u8fc7\u9519\u8bef<\/strong>\uff1a\u5f53\u9047\u5230\u9519\u8bef\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u8df3\u8fc7\u5f53\u524d\u4ee3\u7801\u884c\u6216\u7ee7\u7eed\u6267\u884c\u4e0b\u4e00\u4e2a\u65ad\u70b9\u5904\u7684\u4ee3\u7801\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528PyCharm\u8c03\u8bd5\u5de5\u5177\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u4ee3\u7801\u884c\u5de6\u4fa7\u70b9\u51fb\uff0c\u8bbe\u7f6e\u4e00\u4e2a\u65ad\u70b9\u3002<\/li>\n<li>\u70b9\u51fb\u201cDebug\u201d\u6309\u94ae\u542f\u52a8\u8c03\u8bd5\u6a21\u5f0f\u3002<\/li>\n<li>\u5728\u8c03\u8bd5\u63a7\u5236\u53f0\u4e2d\uff0c\u4f7f\u7528\u201cStep Over\u201d\u6216\u201cResume\u201d\u6309\u94ae\u9010\u884c\u8c03\u8bd5\u6216\u8df3\u8fc7\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u7ec6\u81f4\u5730\u4e86\u89e3\u4ee3\u7801\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u4ece\u800c\u66f4\u6709\u6548\u5730\u5904\u7406\u548c\u8df3\u8fc7\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4fee\u6539\u4ee3\u7801\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u4fee\u6539\u4ee3\u7801\u7ed3\u6784\u4e5f\u80fd\u591f\u907f\u514d\u9519\u8bef\u7684\u53d1\u751f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u66f4\u5065\u58ee\u7684\u4ee3\u7801\u903b\u8f91\u3001\u6dfb\u52a0\u66f4\u591a\u7684\u6761\u4ef6\u5224\u65ad\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad<\/strong>\uff1a\u5728\u6267\u884c\u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801\u524d\uff0c\u5148\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u5b89\u5168\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if denominator != 0:<\/p>\n<p>    result = numerator \/ denominator<\/p>\n<p>else:<\/p>\n<p>    result = None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f18\u5316\u4ee3\u7801\u903b\u8f91<\/strong>\uff1a\u901a\u8fc7\u91cd\u6784\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u52a0\u5065\u58ee\u548c\u53ef\u8bfb\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_divide(numerator, denominator):<\/p>\n<p>    if denominator == 0:<\/p>\n<p>        return None<\/p>\n<p>    return numerator \/ denominator<\/p>\n<p>result = safe_divide(10, 0)<\/p>\n<p>print(result)  # \u8f93\u51faNone<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u80fd\u591f\u5728\u4ee3\u7801\u5c42\u9762\u4e0a\u907f\u514d\u548c\u5904\u7406\u9519\u8bef\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u548c\u5904\u7406\u9519\u8bef\u65f6\uff0c\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u4e86\u89e3\u9519\u8bef\u7684\u53d1\u751f\u539f\u56e0\u548c\u4e0a\u4e0b\u6587\u3002Python\u7684logging\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.ERROR, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError as e:<\/p>\n<p>    logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>    result = None<\/p>\n<p>print(result)  # \u8f93\u51faNone<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5373\u4f7f\u7a0b\u5e8f\u53d1\u751f\u9519\u8bef\uff0c\u4e5f\u80fd\u591f\u901a\u8fc7\u65e5\u5fd7\u8bb0\u5f55\u4e86\u89e3\u9519\u8bef\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4ece\u800c\u66f4\u597d\u5730\u8fdb\u884c\u8c03\u8bd5\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08Context Manager\uff09\u662f\u4e00\u79cdPython\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u80fd\u591f\u5728\u4ee3\u7801\u5757\u7684\u524d\u540e\u6267\u884c\u7279\u5b9a\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u5757\u4e2d\u6355\u83b7\u5e76\u5904\u7406\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from contextlib import contextmanager<\/p>\n<p>@contextmanager<\/p>\n<p>def error_handler():<\/p>\n<p>    try:<\/p>\n<p>        yield<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>with error_handler():<\/p>\n<p>    result = 10 \/ 0  # \u53ef\u80fd\u4ea7\u751fZeroDivisionError<\/p>\n<p>print(result)  # \u8f93\u51faNone<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5728\u4ee3\u7801\u5757\u7684\u524d\u540e\u6267\u884c\u7279\u5b9a\u7684\u64cd\u4f5c\uff0c\u4ece\u800c\u66f4\u7075\u6d3b\u5730\u5904\u7406\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Python\u5185\u7f6e\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u548c\u8df3\u8fc7\u9519\u8bef\u3002\u4f8b\u5982\uff0c<code>retrying<\/code>\u5e93\u80fd\u591f\u81ea\u52a8\u91cd\u8bd5\u5931\u8d25\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>retrying<\/code>\u5e93\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from retrying import retry<\/p>\n<p>@retry(stop_max_attempt_number=3)<\/p>\n<p>def unreliable_function():<\/p>\n<p>    if random.random() &lt; 0.5:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Unreliable function failed&quot;)<\/p>\n<p>    return &quot;Success&quot;<\/p>\n<p>try:<\/p>\n<p>    result = unreliable_function()<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An error occurred after retries: {e}&quot;)<\/p>\n<p>    result = None<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5728\u53d1\u751f\u9519\u8bef\u65f6\u81ea\u52a8\u91cd\u8bd5\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u548c\u8df3\u8fc7\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u4ee3\u7801\u5757\u6355\u83b7\u9519\u8bef\uff0c\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u7ed3\u5408\u65ad\u70b9\u8c03\u8bd5\u5de5\u5177\u8fdb\u884c\u8be6\u7ec6\u8c03\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.ERROR, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>def safe_divide(numerator, denominator):<\/p>\n<p>    try:<\/p>\n<p>        return numerator \/ denominator<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>result = safe_divide(10, 0)<\/p>\n<p>print(result)  # \u8f93\u51faNone<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u80fd\u591f\u66f4\u5168\u9762\u5730\u5904\u7406\u548c\u8c03\u8bd5\u9519\u8bef\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u8c03\u8bd5\u65f6\u8df3\u8fc7\u9519\u8bef\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528try-except\u4ee3\u7801\u5757\u3001\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u5de5\u5177\u3001\u4fee\u6539\u4ee3\u7801\u7ed3\u6784\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5efa\u8bae\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u7efc\u5408\u5904\u7406\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\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>\u5728Python\u8c03\u8bd5\u65f6\uff0c\u6211\u8be5\u5982\u4f55\u5904\u7406\u5e38\u89c1\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728Python\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u5e38\u89c1\u9519\u8bef\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u9996\u5148\uff0c\u4e86\u89e3\u9519\u8bef\u7684\u7c7b\u578b\u975e\u5e38\u91cd\u8981\u3002\u4f7f\u7528try-except\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c06\u53ef\u80fd\u5f15\u53d1\u9519\u8bef\u7684\u4ee3\u7801\u653e\u5728try\u5757\u4e2d\uff0c\u5e76\u5728except\u5757\u4e2d\u5904\u7406\u76f8\u5e94\u7684\u5f02\u5e38\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u9047\u5230\u9519\u8bef\uff0c\u7a0b\u5e8f\u4e5f\u80fd\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u6709\u6548\u5b9a\u4f4d\u9519\u8bef\u6e90\uff1f<\/strong><br \/>\u5b9a\u4f4d\u9519\u8bef\u6e90\u7684\u5173\u952e\u5728\u4e8e\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u548c\u65e5\u5fd7\u8bb0\u5f55\u3002Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684pdb\u6a21\u5757\uff0c\u53ef\u4ee5\u9010\u884c\u6267\u884c\u4ee3\u7801\uff0c\u68c0\u67e5\u53d8\u91cf\u72b6\u6001\u548c\u8c03\u7528\u5806\u6808\u3002\u6b64\u5916\uff0c\u4f7f\u7528print\u8bed\u53e5\u6216logging\u6a21\u5757\u8bb0\u5f55\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u4fe1\u606f\uff0c\u4e5f\u80fd\u5e2e\u52a9\u60a8\u8ffd\u8e2a\u5230\u95ee\u9898\u53d1\u751f\u7684\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5728\u8c03\u8bd5\u65f6\uff0c\u662f\u5426\u6709\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u8df3\u8fc7\u7279\u5b9a\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u6709\u591a\u79cd\u5de5\u5177\u548c\u6280\u672f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8df3\u8fc7\u7279\u5b9a\u7684\u9519\u8bef\u3002\u5728\u4f7f\u7528IDE\uff08\u5982PyCharm\u6216VSCode\uff09\u65f6\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u6761\u4ef6\u65ad\u70b9\uff0c\u4ec5\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u6682\u505c\u7a0b\u5e8f\u6267\u884c\u3002\u4f7f\u7528try-except\u8bed\u53e5\u4e5f\u53ef\u4ee5\u8df3\u8fc7\u7279\u5b9a\u7684\u5f02\u5e38\u5904\u7406\uff0c\u4ece\u800c\u5141\u8bb8\u7a0b\u5e8f\u7ee7\u7eed\u8fd0\u884c\u800c\u4e0d\u4e2d\u65ad\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u7ba1\u7406\u8c03\u8bd5\u8fc7\u7a0b\uff0c\u4e13\u6ce8\u4e8e\u66f4\u91cd\u8981\u7684\u4ee3\u7801\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u8c03\u8bd5\u65f6\u8df3\u8fc7\u9519\u8bef\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528try-except\u4ee3\u7801\u5757\u3001\u4f7f\u7528\u65ad\u70b9\u8c03\u8bd5\u5de5\u5177\u3001\u4fee\u6539\u4ee3\u7801\u7ed3\u6784\u3002\u5176\u4e2d\uff0c\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1051269,"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\/1051256"}],"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=1051256"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051256\/revisions"}],"predecessor-version":[{"id":1051272,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1051256\/revisions\/1051272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1051269"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1051256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1051256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1051256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}