{"id":1062086,"date":"2024-12-31T15:48:43","date_gmt":"2024-12-31T07:48:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1062086.html"},"modified":"2024-12-31T15:48:46","modified_gmt":"2024-12-31T07:48:46","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e9%94%99%e8%af%af%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1062086.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u9519\u8bef\u4fe1\u606f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/35c4e865-c7df-4937-b802-c9ca6ec05e05.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5220\u9664\u9519\u8bef\u4fe1\u606f\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528try-except\u6355\u83b7\u5f02\u5e38\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u548c\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664\u6216\u8005\u5904\u7406\u9519\u8bef\u4fe1\u606f\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a<strong>\u4f7f\u7528try-except\u6355\u83b7\u5f02\u5e38\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u548c\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u5668<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528try-except\u6355\u83b7\u5f02\u5e38\u662f\u6700\u5e38\u7528\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u52a0\u5165\u5f02\u5e38\u6355\u83b7\u673a\u5236\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u5904\u7406\u9519\u8bef\u4fe1\u606f\uff0c\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528try-except\u6355\u83b7\u5f02\u5e38<\/h2>\n<\/p>\n<p><h3>1.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u901a\u8fc7try-except\u8bed\u53e5\uff0c\u53ef\u4ee5\u6355\u83b7\u7a0b\u5e8f\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u53d1\u751f\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError as e:<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>ZeroDivisionError<\/code> \u5f02\u5e38\u88ab\u6355\u83b7\uff0c\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\u3002\u8fd9\u6837\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u5f02\u5e38\u800c\u5d29\u6e83\uff0c\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u6355\u83b7\u591a\u79cd\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u4e00\u4e2a\u4ee3\u7801\u5757\u53ef\u80fd\u4f1a\u4ea7\u751f\u591a\u79cd\u4e0d\u540c\u7684\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u5728except\u8bed\u53e5\u4e2d\u6307\u5b9a\u591a\u4e2a\u5f02\u5e38\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except (ZeroDivisionError, TypeError) as e:<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.3 \u6355\u83b7\u6240\u6709\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u786e\u5b9a\u4f1a\u53d1\u751f\u54ea\u79cd\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528\u901a\u7528\u7684 <code>Exception<\/code> \u7c7b\u6765\u6355\u83b7\u6240\u6709\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.4 else\u548cfinally\u5b50\u53e5<\/h3>\n<\/p>\n<p><p>\u5728try-except\u8bed\u53e5\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528else\u548cfinally\u5b50\u53e5\uff0c\u5206\u522b\u8868\u793a\u65e0\u5f02\u5e38\u65f6\u6267\u884c\u7684\u4ee3\u7801\u5757\u548c\u65e0\u8bba\u6709\u65e0\u5f02\u5e38\u90fd\u6267\u884c\u7684\u4ee3\u7801\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 2<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38\uff1a{e}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u7ed3\u679c\u662f\uff1a{result}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    print(&quot;\u6267\u884c\u5b8c\u6210&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55<\/h2>\n<\/p>\n<p><h3>2.1 \u65e5\u5fd7\u6a21\u5757\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p>Python\u7684\u65e5\u5fd7\u6a21\u5757\uff08logging\uff09\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8bb0\u5f55\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u5305\u62ec\u9519\u8bef\u4fe1\u606f\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u65e5\u5fd7\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u76d1\u63a7\u548c\u5206\u6790\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>2.2 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5f15\u5165logging\u6a21\u5757\uff0c\u5e76\u8fdb\u884c\u57fa\u672c\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u914d\u7f6e\u65e5\u5fd7<\/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;\u6355\u83b7\u5230\u5f02\u5e38\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u65e5\u5fd7\u7ea7\u522b\u88ab\u8bbe\u7f6e\u4e3aERROR\uff0c\u53ea\u6709ERROR\u53ca\u4ee5\u4e0a\u7ea7\u522b\u7684\u4fe1\u606f\u4f1a\u88ab\u8bb0\u5f55\u3002\u901a\u8fc7\u65e5\u5fd7\u683c\u5f0f\u5316\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770b\u65e5\u5fd7\u7684\u65f6\u95f4\u3001\u7ea7\u522b\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>2.3 \u65e5\u5fd7\u7ea7\u522b<\/h3>\n<\/p>\n<p><p>logging\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u4e2a\u65e5\u5fd7\u7ea7\u522b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7ea7\u522b\u8fdb\u884c\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<ul>\n<li>DEBUG\uff1a\u8c03\u8bd5\u4fe1\u606f<\/li>\n<li>INFO\uff1a\u4e00\u822c\u4fe1\u606f<\/li>\n<li>WARNING\uff1a\u8b66\u544a\u4fe1\u606f<\/li>\n<li>ERROR\uff1a\u9519\u8bef\u4fe1\u606f<\/li>\n<li>CRITICAL\uff1a\u4e25\u91cd\u9519\u8bef\u4fe1\u606f<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">logging.debug(&quot;\u8fd9\u662f\u8c03\u8bd5\u4fe1\u606f&quot;)<\/p>\n<p>logging.info(&quot;\u8fd9\u662f\u4e00\u822c\u4fe1\u606f&quot;)<\/p>\n<p>logging.warning(&quot;\u8fd9\u662f\u8b66\u544a\u4fe1\u606f&quot;)<\/p>\n<p>logging.error(&quot;\u8fd9\u662f\u9519\u8bef\u4fe1\u606f&quot;)<\/p>\n<p>logging.critical(&quot;\u8fd9\u662f\u4e25\u91cd\u9519\u8bef\u4fe1\u606f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u5668<\/h2>\n<\/p>\n<p><h3>3.1 \u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6807\u51c6\u7684\u5f02\u5e38\u7c7b\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u6765\u5904\u7406\u7279\u5b9a\u7684\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CustomError(Exception):<\/p>\n<p>    def __init__(self, message):<\/p>\n<p>        self.message = message<\/p>\n<p>        super().__init__(self.message)<\/p>\n<p>try:<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se CustomError(&quot;\u8fd9\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38&quot;)<\/p>\n<p>except CustomError as e:<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u5728\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u4e2d\u6dfb\u52a0\u66f4\u591a\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u4e0d\u4ec5\u53ef\u4ee5\u5305\u542b\u9519\u8bef\u4fe1\u606f\uff0c\u8fd8\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u6709\u7528\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u9519\u8bef\u4ee3\u7801\u3001\u5efa\u8bae\u89e3\u51b3\u65b9\u6848\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CustomError(Exception):<\/p>\n<p>    def __init__(self, code, message):<\/p>\n<p>        self.code = code<\/p>\n<p>        self.message = message<\/p>\n<p>        super().__init__(self.message)<\/p>\n<p>try:<\/p>\n<p>    raise CustomError(404, &quot;\u8d44\u6e90\u672a\u627e\u5230&quot;)<\/p>\n<p>except CustomError as e:<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a\u4ee3\u7801{e.code}\uff0c\u4fe1\u606f\uff1a{e.message}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c\u5e76\u63d0\u4f9b\u66f4\u591a\u6709\u7528\u7684\u4fe1\u606f\u5e2e\u52a9\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u7ed3\u5408\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h2>\n<\/p>\n<p><h3>4.1 \u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684\u673a\u5236\uff0c\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u5305\u62ec\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u3002\u901a\u8fc7\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u786e\u4fdd\u8d44\u6e90\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u5f97\u5230\u6b63\u786e\u7684\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><h3>4.2 \u4f7f\u7528with\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>Python\u7684with\u8bed\u53e5\u53ef\u4ee5\u7b80\u5316\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684\u4f7f\u7528\uff0c\u907f\u514d\u624b\u52a8\u7ba1\u7406\u8d44\u6e90\u91ca\u653e\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;non_existent_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>except FileNotFoundError as e:<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528with\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u4f1a\u6355\u83b7\u5230FileNotFoundError\u5f02\u5e38\uff0c\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>4.3 \u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49__enter__\u548c__exit__\u65b9\u6cd5\u6765\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u65f6\u81ea\u52a8\u5904\u7406\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CustomContextManager:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&quot;\u8fdb\u5165\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&quot;)<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        print(&quot;\u9000\u51fa\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&quot;)<\/p>\n<p>        if exc_type:<\/p>\n<p>            print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38\uff1a{exc_value}&quot;)<\/p>\n<p>        return True  # \u8fd4\u56deTrue\u8868\u793a\u5f02\u5e38\u5df2\u7ecf\u88ab\u5904\u7406<\/p>\n<p>try:<\/p>\n<p>    with CustomContextManager():<\/p>\n<p>        result = 10 \/ 0<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;\u672a\u5904\u7406\u7684\u5f02\u5e38\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u81ea\u5b9a\u4e49\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4f1a\u5728\u8fdb\u5165\u548c\u9000\u51fa\u65f6\u5206\u522b\u6253\u5370\u63d0\u793a\u4fe1\u606f\uff0c\u5e76\u5728\u9000\u51fa\u65f6\u6355\u83b7\u5f02\u5e38\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5220\u9664\u6216\u8005\u5904\u7406Python\u4e2d\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u7a33\u5b9a\u8fd0\u884c\u3002<strong>\u4f7f\u7528try-except\u6355\u83b7\u5f02\u5e38\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u548c\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u5668<\/strong>\u662f\u5e38\u7528\u7684\u4e09\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u7279\u70b9\u3002\u7ed3\u5408\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7b80\u5316<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6355\u83b7\u5e76\u5904\u7406\u9519\u8bef\u4fe1\u606f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u9519\u8bef\u4fe1\u606f\u5e76\u5904\u7406\u5b83\u4eec\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u9519\u8bef\u800c\u5d29\u6e83\uff0c\u540c\u65f6\u53ef\u4ee5\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u6216\u8005\u7ed9\u51fa\u7528\u6237\u53cb\u597d\u7684\u63d0\u793a\u3002\u5177\u4f53\u505a\u6cd5\u662f\u5728try\u5757\u4e2d\u653e\u5165\u53ef\u80fd\u5f15\u53d1\u9519\u8bef\u7684\u4ee3\u7801\uff0c\u800c\u5728except\u5757\u4e2d\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u6765\u7ba1\u7406\u9519\u8bef\u4fe1\u606f\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684logging\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u7684\u6709\u6548\u65b9\u5f0f\u3002\u901a\u8fc7\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\uff0c\u53ef\u4ee5\u5c06\u9519\u8bef\u4fe1\u606f\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3001\u6587\u4ef6\u6216\u5176\u4ed6\u65e5\u5fd7\u5904\u7406\u7cfb\u7edf\u4e2d\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u4e0d\u5e72\u6270\u7528\u6237\u4f53\u9a8c\u7684\u60c5\u51b5\u4e0b\uff0c\u4fdd\u7559\u9519\u8bef\u4fe1\u606f\u4ee5\u5907\u540e\u7eed\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fddPython\u7a0b\u5e8f\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u4fdd\u6301\u7a33\u5b9a\uff1f<\/strong><br \/>\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u5e38\u89c1\u9519\u8bef\uff0c\u5e76\u4f7f\u7528finally\u5757\u6765\u6267\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5373\u4f7f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e5f\u80fd\u591f\u6b63\u5e38\u5173\u95ed\u6216\u91ca\u653e\u8d44\u6e90\u3002\u6b64\u5916\uff0c\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u63d0\u524d\u53d1\u73b0\u6f5c\u5728\u95ee\u9898\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528try-except\u6355\u83b7\u5f02\u5e38\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u548c\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u5668 \u5728Python\u4e2d\u5220\u9664\u6216\u8005\u5904\u7406\u9519\u8bef\u4fe1\u606f\u662f\u4e00\u4e2a\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1062097,"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\/1062086"}],"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=1062086"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062086\/revisions"}],"predecessor-version":[{"id":1062098,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062086\/revisions\/1062098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1062097"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1062086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1062086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1062086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}