{"id":1176513,"date":"2025-01-15T17:45:37","date_gmt":"2025-01-15T09:45:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176513.html"},"modified":"2025-01-15T17:45:41","modified_gmt":"2025-01-15T09:45:41","slug":"python%e5%a6%82%e4%bd%95%e6%8d%95%e6%8d%89%e6%89%80%e6%9c%89%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1176513.html","title":{"rendered":"python\u5982\u4f55\u6355\u6349\u6240\u6709\u9519\u8bef"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25111652\/394aa429-2f60-465f-9082-9f06d2a5009e.webp\" alt=\"python\u5982\u4f55\u6355\u6349\u6240\u6709\u9519\u8bef\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u6355\u6349\u6240\u6709\u9519\u8bef\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4f7f\u7528<strong>try-except<\/strong>\u7ed3\u6784\uff0c\u5e76\u5728except\u5757\u4e2d\u6355\u6349<strong>Exception<\/strong>\u7c7b\u3002<strong>Exception<\/strong>\u7c7b\u662f\u6240\u6709\u5185\u7f6e\u5f02\u5e38\u7684\u57fa\u7c7b\uff0c\u56e0\u6b64\u53ef\u4ee5\u6355\u6349\u5230\u6240\u6709\u6807\u51c6\u5f02\u5e38\u3002<strong>\u6ce8\u610f\uff1a\u6355\u6349\u6240\u6709\u9519\u8bef\u65f6\uff0c\u8981\u8c28\u614e\u5904\u7406\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027<\/strong>\u3002\u4e3a\u4e86\u8be6\u7ec6\u63cf\u8ff0\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u6355\u6349\u6240\u6709\u9519\u8bef\u3001\u5982\u4f55\u5904\u7406\u9519\u8bef\u4ee5\u53ca\u5982\u4f55\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4ea7\u751f\u5f02\u5e38\u7684\u4ee3\u7801\u5757<\/p>\n<p>except Exception as e:<\/p>\n<p>    # \u6355\u6349\u6240\u6709\u5f02\u5e38<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u6355\u6349\u6240\u6709\u9519\u8bef\u7684\u57fa\u7840<\/h3>\n<\/p>\n<p><p>\u5728\u6355\u6349\u6240\u6709\u9519\u8bef\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3Python\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002\u5f02\u5e38\u5904\u7406\u662f\u6307\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u5904\u7406\u7a0b\u5e8f\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\u3002Python\u63d0\u4f9b\u4e86\u4e00\u5957\u5b8c\u6574\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u5305\u62ectry\u3001except\u3001finally\u3001r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se\u7b49\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7ed3\u6784<\/h4>\n<\/p>\n<p><p><strong>try-except<\/strong>\u7ed3\u6784\u662f\u6700\u5e38\u89c1\u7684\u5f02\u5e38\u5904\u7406\u65b9\u5f0f\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u6355\u6349\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4ea7\u751f\u5f02\u5e38\u7684\u4ee3\u7801\u5757<\/p>\n<p>except Exception as e:<\/p>\n<p>    # \u6355\u6349\u6240\u6709\u5f02\u5e38<\/p>\n<p>    print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\uff0ctry\u5757\u4e2d\u7684\u4ee3\u7801\u4f1a\u88ab\u6267\u884c\uff0c\u5982\u679c\u51fa\u73b0\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u8df3\u5230except\u5757\uff0c\u5e76\u5c06\u5f02\u5e38\u4fe1\u606f\u8d4b\u503c\u7ed9\u53d8\u91cfe\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6355\u6349\u591a\u4e2a\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u6355\u6349\u591a\u4e2a\u7279\u5b9a\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2aexcept\u5757\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4ea7\u751f\u5f02\u5e38\u7684\u4ee3\u7801\u5757<\/p>\n<p>except ValueError as ve:<\/p>\n<p>    print(f&quot;ValueError occurred: {ve}&quot;)<\/p>\n<p>except TypeError as te:<\/p>\n<p>    print(f&quot;TypeError occurred: {te}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;An unexpected error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u5f02\u5e38\u7c7b\u578b\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\uff0c\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6355\u6349\u5230\u9519\u8bef\u540e\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7\uff0c\u4ee5\u4fbf\u540e\u7eed\u6392\u67e5\u95ee\u9898\u3002Python\u63d0\u4f9b\u4e86logging\u6a21\u5757\uff0c\u65b9\u4fbf\u6211\u4eec\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7684\u57fa\u672c\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(filename=&#39;error.log&#39;, level=logging.ERROR,<\/p>\n<p>                    format=&#39;%(asctime)s:%(levelname)s:%(message)s&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u914d\u7f6e\u4e2d\uff0c\u65e5\u5fd7\u4f1a\u88ab\u8bb0\u5f55\u5230error.log\u6587\u4ef6\u4e2d\uff0c\u65e5\u5fd7\u7ea7\u522b\u4e3aERROR\uff0c\u5e76\u4e14\u5305\u542b\u65f6\u95f4\u3001\u65e5\u5fd7\u7ea7\u522b\u548c\u65e5\u5fd7\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8bb0\u5f55\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728except\u5757\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528logging\u6a21\u5757\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4ea7\u751f\u5f02\u5e38\u7684\u4ee3\u7801\u5757<\/p>\n<p>except Exception as e:<\/p>\n<p>    logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5f02\u5e38\u4fe1\u606f\u4f1a\u88ab\u8bb0\u5f55\u5230\u6307\u5b9a\u7684\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u7279\u5b9a\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u7279\u5b9a\u7684\u5f02\u5e38\u8fdb\u884c\u5904\u7406\uff0c\u6bd4\u5982\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25\u3001\u6587\u4ef6\u64cd\u4f5c\u9519\u8bef\u7b49\u3002\u8fd9\u65f6\u53ef\u4ee5\u901a\u8fc7\u6355\u6349\u7279\u5b9a\u5f02\u5e38\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7f51\u7edc\u8bf7\u6c42\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7f51\u7edc\u8bf7\u6c42\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528requests\u5e93\uff0c\u5e76\u6355\u6349\u76f8\u5173\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<p>    response.raise_for_status()<\/p>\n<p>except requests.exceptions.HTTPError as http_err:<\/p>\n<p>    logging.error(f&quot;HTTP error occurred: {http_err}&quot;)<\/p>\n<p>except requests.exceptions.ConnectionError as conn_err:<\/p>\n<p>    logging.error(f&quot;Connection error occurred: {conn_err}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6587\u4ef6\u64cd\u4f5c\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6587\u4ef6\u64cd\u4f5c\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\uff0c\u5e76\u6355\u6349\u76f8\u5173\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;nonexistent_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>except FileNotFoundError as fnf_err:<\/p>\n<p>    logging.error(f&quot;File not found: {fnf_err}&quot;)<\/p>\n<p>except IOError as io_err:<\/p>\n<p>    logging.error(f&quot;IO error occurred: {io_err}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8d44\u6e90\u6e05\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5f02\u5e38\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u8fdb\u884c\u8d44\u6e90\u6e05\u7406\uff0c\u6bd4\u5982\u5173\u95ed\u6587\u4ef6\u3001\u5173\u95ed\u7f51\u7edc\u8fde\u63a5\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528finally\u5757\u6765\u5b8c\u6210\u8d44\u6e90\u6e05\u7406\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4ea7\u751f\u5f02\u5e38\u7684\u4ee3\u7801\u5757<\/p>\n<p>except Exception as e:<\/p>\n<p>    logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    # \u8d44\u6e90\u6e05\u7406\u4ee3\u7801<\/p>\n<p>    print(&quot;Cleaning up resources&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\uff0c\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0cfinally\u5757\u4e2d\u7684\u4ee3\u7801\u90fd\u4f1a\u88ab\u6267\u884c\uff0c\u786e\u4fdd\u8d44\u6e90\u5f97\u5230\u6b63\u786e\u7684\u6e05\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5904\u7406\u7279\u5b9a\u7684\u9519\u8bef\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u53ef\u4ee5\u7ee7\u627f\u81eaException\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyCustomError(Exception):<\/p>\n<p>    def __init__(self, message):<\/p>\n<p>        self.message = message<\/p>\n<p>        super().__init__(self.message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u89e6\u53d1\u81ea\u5b9a\u4e49\u5f02\u5e38<\/p>\n<p>    raise MyCustomError(&quot;This is a custom error&quot;)<\/p>\n<p>except MyCustomError as custom_err:<\/p>\n<p>    logging.error(f&quot;Custom error occurred: {custom_err}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    logging.error(f&quot;An error occurred: {e}&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\u7279\u5b9a\u7684\u9519\u8bef\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08with\u8bed\u53e5\uff09\u6765\u7b80\u5316<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\uff0c\u6bd4\u5982\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6587\u4ef6\u64cd\u4f5c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>except FileNotFoundError as fnf_err:<\/p>\n<p>    logging.error(f&quot;File not found: {fnf_err}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u5e93\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u786e\u4fdd\u8fde\u63a5\u7684\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>try:<\/p>\n<p>    with sqlite3.connect(&#39;example.db&#39;) as conn:<\/p>\n<p>        cursor = conn.cursor()<\/p>\n<p>        cursor.execute(&quot;SELECT * FROM example_table&quot;)<\/p>\n<p>        rows = cursor.fetchall()<\/p>\n<p>except sqlite3.DatabaseError as db_err:<\/p>\n<p>    logging.error(f&quot;Database error occurred: {db_err}&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5f02\u5e38\u94fe<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5f02\u5e38\u65f6\uff0c\u6709\u65f6\u9700\u8981\u4fdd\u7559\u539f\u59cb\u5f02\u5e38\u4fe1\u606f\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u94fe\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    try:<\/p>\n<p>        1 \/ 0<\/p>\n<p>    except ZeroDivisionError as zde:<\/p>\n<p>        raise ValueError(&quot;Value error occurred&quot;) from zde<\/p>\n<p>except Exception as e:<\/p>\n<p>    logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\uff0c\u539f\u59cb\u5f02\u5e38ZeroDivisionError\u4f1a\u88ab\u4fdd\u7559\uff0c\u5e76\u4f5c\u4e3aValueError\u5f02\u5e38\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86Python\u4e2d\u5982\u4f55\u6355\u6349\u6240\u6709\u9519\u8bef\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u3002<strong>\u6355\u6349\u6240\u6709\u9519\u8bef\u7684\u6838\u5fc3\u5728\u4e8e\u4f7f\u7528try-except\u7ed3\u6784\u5e76\u6355\u6349Exception\u7c7b<\/strong>\uff0c\u5e76\u7ed3\u5408\u65e5\u5fd7\u8bb0\u5f55\u3001\u7279\u5b9a\u5f02\u5e38\u5904\u7406\u3001\u8d44\u6e90\u6e05\u7406\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u548c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7b49\u6280\u672f\u624b\u6bb5\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c<strong>\u5408\u7406\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027<\/strong>\uff0c\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5e94\u5bf9\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u8ba9\u4f60\u5728\u7f16\u5199Python\u7a0b\u5e8f\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5168\u5c40\u5f02\u5e38\u5904\u7406\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u7684<code>excepthook<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u5168\u5c40\u5f02\u5e38\u5904\u7406\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49<code>sys.excepthook<\/code>\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u6355\u6349\u672a\u5904\u7406\u7684\u5f02\u5e38\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\uff0c\u6bd4\u5982\u8bb0\u5f55\u65e5\u5fd7\u6216\u53d1\u9001\u8b66\u62a5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import sys\n\ndef handle_exception(exc_type, exc_value, exc_traceback):\n    print(&quot;Caught an exception:&quot;, exc_value)\n\nsys.excepthook = handle_exception\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u6355\u6349\u7279\u5b9a\u7c7b\u578b\u7684\u9519\u8bef\u6709\u54ea\u4e9b\u65b9\u6cd5\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u6349\u7279\u5b9a\u7c7b\u578b\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u6355\u6349<code>ValueError<\/code>\u6216<code>IOError<\/code>\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    x = int(&quot;abc&quot;)  # \u53ef\u80fd\u4f1a\u5f15\u53d1ValueError\nexcept ValueError as e:\n    print(&quot;ValueError caught:&quot;, e)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528logging\u6a21\u5757\u8bb0\u5f55\u6355\u6349\u5230\u7684\u9519\u8bef\u4fe1\u606f\uff1f<\/strong><br \/>\u4f7f\u7528Python\u7684<code>logging<\/code>\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8bb0\u5f55\u6355\u6349\u5230\u7684\u9519\u8bef\u4fe1\u606f\u3002\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u548c\u683c\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5c06\u9519\u8bef\u4fe1\u606f\u8f93\u51fa\u5230\u6587\u4ef6\u6216\u63a7\u5236\u53f0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import logging\n\nlogging.basicConfig(filename=&#39;error.log&#39;, level=logging.ERROR)\n\ntry:\n    result = 10 \/ 0  # \u4f1a\u5f15\u53d1ZeroDivisionError\nexcept Exception as e:\n    logging.error(&quot;An error occurred: %s&quot;, e)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u505a\u6cd5\u80fd\u591f\u5e2e\u52a9\u60a8\u5728\u53d1\u751f\u9519\u8bef\u65f6\u4fdd\u7559\u8bb0\u5f55\uff0c\u65b9\u4fbf\u540e\u7eed\u7684\u8c03\u8bd5\u4e0e\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6355\u6349\u6240\u6709\u9519\u8bef\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4f7f\u7528try-except\u7ed3\u6784\uff0c\u5e76\u5728except\u5757\u4e2d\u6355\u6349Exceptio [&hellip;]","protected":false},"author":3,"featured_media":1176519,"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\/1176513"}],"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=1176513"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176513\/revisions"}],"predecessor-version":[{"id":1176520,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176513\/revisions\/1176520"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1176519"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1176513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1176513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1176513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}