{"id":934,"date":"2025-01-12T22:29:30","date_gmt":"2025-01-12T14:29:30","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=934"},"modified":"2025-01-12T22:29:30","modified_gmt":"2025-01-12T14:29:30","slug":"python-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5%ef%bc%9a%e5%85%a8%e9%9d%a2%e6%8e%8c%e6%8f%a1%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e6%8a%80%e5%b7%a7%e4%b8%8e%e4%bc%98","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/12\/python-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5%ef%bc%9a%e5%85%a8%e9%9d%a2%e6%8e%8c%e6%8f%a1%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e6%8a%80%e5%b7%a7%e4%b8%8e%e4%bc%98\/","title":{"rendered":"Python \u9519\u8bef\u5904\u7406\u6700\u4f73\u5b9e\u8df5\uff1a\u5168\u9762\u638c\u63e1\u5f02\u5e38\u5904\u7406\u6280\u5de7\u4e0e\u4f18\u5316\u65b9\u6cd5"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\uff0c\u9519\u8bef\u5904\u7406\u662f\u7f16\u7a0b\u4e2d\u81f3\u5173\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u5408\u7406\u7684\u9519\u8bef\u5904\u7406\u80fd\u591f\u589e\u5f3a\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002\u4ee5\u4e0b\u662f Python \u9519\u8bef\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9\u4f60\u6709\u6548\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u57fa\u672c\u5f02\u5e38\u5904\u7406\uff1a<code>try<\/code>\/<code>except<\/code> \u8bed\u53e5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>try<\/code>\/<code>except<\/code> \u8bed\u53e5\u662f Python \u5f02\u5e38\u5904\u7406\u7684\u57fa\u7840\u3002\u5728\u4ee3\u7801\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u90e8\u5206\u4f7f\u7528 <code>try<\/code> \u5757\uff0c\u5728\u53d1\u751f\u7279\u5b9a\u5f02\u5e38\u65f6\u6355\u6349\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u672c\u8bed\u6cd5\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    # \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\nexcept SomeException as e:\n    # \u5f02\u5e38\u5904\u7406\u4ee3\u7801<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\uff1a<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6709\u4e00\u4e2a\u4ece\u7528\u6237\u8f93\u5165\u83b7\u53d6\u6574\u6570\u7684\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    user_input = int(input(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff1a\"))\nexcept ValueError as e:\n    print(\"\u8f93\u5165\u9519\u8bef\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6570\u5b57\u3002\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u6709\u6548\u6355\u83b7\u9519\u8bef\uff0c\u907f\u514d\u7a0b\u5e8f\u76f4\u63a5\u5d29\u6e83\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u540c\u7684\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a <code>except<\/code> \u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u3002\u8fd9\u6837\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7684\u9519\u8bef\u63d0\u4f9b\u9488\u5bf9\u6027\u7684\u5904\u7406\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bed\u6cd5\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    # \u53ef\u80fd\u629b\u51fa\u591a\u4e2a\u5f02\u5e38\u7684\u4ee3\u7801\nexcept ValueError as e:\n    # \u5904\u7406 ValueError\nexcept ZeroDivisionError as e:\n    # \u5904\u7406\u9664\u96f6\u9519\u8bef<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    a = int(input(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff1a\"))\n    b = int(input(\"\u8bf7\u8f93\u5165\u53e6\u4e00\u4e2a\u6570\u5b57\uff1a\"))\n    result = a \/ b\nexcept ValueError:\n    print(\"\u8f93\u5165\u65e0\u6548\uff0c\u8bf7\u8f93\u5165\u6570\u5b57\u3002\")\nexcept ZeroDivisionError:\n    print(\"\u9519\u8bef\uff1a\u4e0d\u80fd\u9664\u4ee5\u96f6\uff01\")\nelse:\n    print(f\"\u8ba1\u7b97\u7ed3\u679c\u662f: {result}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5206\u522b\u6355\u83b7\u4e86 <code>ValueError<\/code> \u548c <code>ZeroDivisionError<\/code>\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u4e86\u5177\u4f53\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>else<\/code> \u5b50\u53e5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>else<\/code> \u5b50\u53e5\u7528\u4e8e\u5728\u6ca1\u6709\u629b\u51fa\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u7684\u4ee3\u7801\u5757\u3002\u5b83\u5728 <code>try<\/code> \u5757\u6210\u529f\u6267\u884c\u65f6\u6267\u884c\uff0c\u4f46\u5982\u679c <code>try<\/code> \u5757\u4e2d\u7684\u4ee3\u7801\u629b\u51fa\u5f02\u5e38\uff0c<code>else<\/code> \u8bed\u53e5\u5c06\u88ab\u8df3\u8fc7\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bed\u6cd5\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    # \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\nexcept SomeException:\n    # \u5f02\u5e38\u5904\u7406\u4ee3\u7801\nelse:\n    # \u5982\u679c\u6ca1\u6709\u5f02\u5e38\u53d1\u751f\uff0c\u6267\u884c\u6b64\u4ee3\u7801<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    file = open(\"example.txt\", \"r\")\n    content = file.read()\nexcept FileNotFoundError:\n    print(\"\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002\")\nelse:\n    print(\"\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u6210\u529f\uff1a\")\n    print(content)\nfinally:\n    if 'file' in locals():\n        file.close()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6ca1\u6709\u629b\u51fa\u5f02\u5e38\u65f6\uff0c<code>else<\/code> \u5757\u7684\u5185\u5bb9\u4f1a\u6267\u884c\uff0c\u5b8c\u6210\u6587\u4ef6\u5185\u5bb9\u7684\u8f93\u51fa\u3002<code>finally<\/code> \u786e\u4fdd\u6587\u4ef6\u603b\u662f\u88ab\u5173\u95ed\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong><code>finally<\/code> \u5b50\u53e5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>finally<\/code> \u8bed\u53e5\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u4f1a\u6267\u884c\uff0c\u5e38\u7528\u4e8e\u6e05\u7406\u8d44\u6e90\u3001\u5173\u95ed\u6587\u4ef6\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u64cd\u4f5c\u3002\u65e0\u8bba <code>try<\/code> \u5757\u662f\u5426\u6267\u884c\u6210\u529f\uff0c<code>finally<\/code> \u603b\u4f1a\u6267\u884c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bed\u6cd5\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    # \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\nexcept SomeException:\n    # \u5f02\u5e38\u5904\u7406\u4ee3\u7801\nfinally:\n    # \u6e05\u7406\u4ee3\u7801\uff0c\u4e0d\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    f = open(\"data.txt\", \"w\")\n    f.write(\"Hello, world!\")\nexcept IOError as e:\n    print(f\"\u6587\u4ef6\u64cd\u4f5c\u9519\u8bef: {e}\")\nfinally:\n    f.close()  # \u6587\u4ef6\u603b\u662f\u4f1a\u88ab\u5173\u95ed\uff0c\u5373\u4f7f\u53d1\u751f\u4e86\u5f02\u5e38<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6839\u636e\u7279\u5b9a\u4e1a\u52a1\u903b\u8f91\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u8fd9\u6709\u52a9\u4e8e\u63d0\u4f9b\u66f4\u5177\u8bed\u4e49\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bed\u6cd5\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyCustomError(Exception):\n    def __init__(self, message):\n        self.message = message\n        super().__init__(self.message)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class NegativeNumberError(Exception):\n    def __init__(self, value):\n        self.value = value\n        self.message = f\"\u4e0d\u80fd\u5904\u7406\u8d1f\u6570: {value}\"\n        super().__init__(self.message)\n\ndef calculate_square_root(value):\n    if value &lt; 0:\n        raise NegativeNumberError(value)\n    return value ** 0.5\n\ntry:\n    result = calculate_square_root(-10)\nexcept NegativeNumberError as e:\n    print(f\"\u9519\u8bef: {e}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u5f02\u5e38\u4fe1\u606f\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u64cd\u4f5c\u6027\uff0c\u4e5f\u80fd\u63d0\u4f9b\u66f4\u6709\u610f\u4e49\u7684\u9519\u8bef\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u5f02\u5e38\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u4e0d\u4ec5\u4ec5\u662f\u6355\u83b7\u548c\u663e\u793a\u9519\u8bef\uff0c\u5f80\u5f80\u8fd8\u9700\u8981\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff0c\u4ee5\u4fbf\u540e\u7eed\u6392\u67e5\u3002Python \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684 <code>logging<\/code> \u6a21\u5757\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u5c06\u9519\u8bef\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u57fa\u672c\u4f7f\u7528\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import logging\n\nlogging.basicConfig(filename='app.log', level=logging.ERROR)\n\ntry:\n    1 \/ 0\nexcept ZeroDivisionError as e:\n    logging.error(\"\u53d1\u751f\u9519\u8bef: %s\", e, exc_info=True)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u7684\u7ea7\u522b\uff0c\u53ef\u4ee5\u5c06\u4e0d\u540c\u4e25\u91cd\u5ea6\u7684\u65e5\u5fd7\u4fe1\u606f\u4fdd\u5b58\u4e0b\u6765\u3002\u4f8b\u5982\uff0c<code>logging.ERROR<\/code> \u4f1a\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff0c\u800c <code>logging.DEBUG<\/code> \u5219\u53ef\u4ee5\u7528\u4e8e\u8bb0\u5f55\u8c03\u8bd5\u4fe1\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u91cd\u65b0\u629b\u51fa\u5f02\u5e38<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\u5728\u6355\u83b7\u5f02\u5e38\u540e\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5bf9\u5f02\u5e38\u8fdb\u884c\u90e8\u5206\u5904\u7406\u540e\u91cd\u65b0\u629b\u51fa\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5f02\u5e38\u88ab\u9002\u5f53\u5730\u4f20\u64ad\uff0c\u6216\u8005\u5728\u6355\u83b7\u540e\u505a\u4e00\u4e9b\u540e\u7eed\u6e05\u7406\u5de5\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bed\u6cd5\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    # \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\nexcept SomeException as e:\n    # \u5904\u7406\u5f02\u5e38\n    raise  # \u91cd\u65b0\u629b\u51fa\u5f02\u5e38<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def read_file(file_name):\n    try:\n        with open(file_name, \"r\") as f:\n            return f.read()\n    except FileNotFoundError as e:\n        print(f\"\u9519\u8bef: \u6587\u4ef6 {file_name} \u672a\u627e\u5230\")\n        raise  # \u91cd\u65b0\u629b\u51fa\u5f02\u5e38\u4ee5\u4fbf\u4e0a\u5c42\u8c03\u7528\u8005\u5904\u7406<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u94fe<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\uff0c\u6355\u83b7\u5230\u7684\u5f02\u5e38\u53ef\u80fd\u4f1a\u5173\u8054\u5176\u4ed6\u5f02\u5e38\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u8ffd\u8e2a\u5f02\u5e38\u7684\u6839\u672c\u539f\u56e0\uff0cPython \u5141\u8bb8\u901a\u8fc7 <code>raise ... from<\/code> \u8bed\u6cd5\u6765\u521b\u5efa\u5f02\u5e38\u94fe\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bed\u6cd5\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    # \u67d0\u4e9b\u4ee3\u7801\nexcept SomeException as e:\n    raise NewException(\"\u53d1\u751f\u4e86\u65b0\u7684\u9519\u8bef\") from e<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def process_data(data):\n    if not isinstance(data, list):\n        raise TypeError(\"\u6570\u636e\u5fc5\u987b\u662f\u5217\u8868\")\n    if not data:\n        raise ValueError(\"\u6570\u636e\u4e0d\u80fd\u4e3a\u7a7a\")\n    # \u5904\u7406\u6570\u636e\n    return data\n\ntry:\n    process_data(\"Not a list\")\nexcept Exception as e:\n    raise RuntimeError(\"\u6570\u636e\u5904\u7406\u5931\u8d25\") from e<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u4fdd\u7559\u539f\u59cb\u5f02\u5e38\u4fe1\u606f\uff0c\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u8ffd\u8e2a\u9519\u8bef\u6765\u6e90\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>\u4f7f\u7528 <code>assert<\/code> \u8fdb\u884c\u5f00\u53d1\u65f6\u7684\u8c03\u8bd5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>assert<\/code> \u7528\u4e8e\u6761\u4ef6\u68c0\u67e5\uff0c\u5982\u679c\u6761\u4ef6\u4e0d\u6210\u7acb\uff0c\u5219\u629b\u51fa <code>AssertionError<\/code> \u5f02\u5e38\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u5728\u5f00\u53d1\u9636\u6bb5\u9a8c\u8bc1\u5047\u8bbe\u6761\u4ef6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bed\u6cd5\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>assert condition, \"\u9519\u8bef\u4fe1\u606f\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def divide(a, b):\n    assert b != 0, \"\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\"\n    return a \/ b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u786e\u4fdd\u7a0b\u5e8f\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u9636\u6bb5\u7684\u6b63\u786e\u6027\u3002\u5bf9\u4e8e\u751f\u4ea7\u73af\u5883\uff0c\u4e00\u822c\u4e0d\u4f1a\u4f7f\u7528 <code>assert<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">10. <strong>\u9884\u9632\u6027\u9519\u8bef\u5904\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u63d0\u524d\u68c0\u67e5\u8f93\u5165\u6570\u636e\u548c\u72b6\u6001\uff0c\u80fd\u591f\u6709\u6548\u907f\u514d\u67d0\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u53d1\u751f\u3002\u5bf9\u4e8e\u53ef\u80fd\u5bfc\u81f4\u5f02\u5e38\u7684\u60c5\u51b5\uff0c\u6700\u597d\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u63d0\u524d\u6355\u83b7\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u9645\u5e94\u7528\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def safe_divide(a, b):\n    if b == 0:\n        print(\"\u9519\u8bef\uff1a\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\")\n        return None\n    return a \/ b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u907f\u514d\u56e0\u9664\u96f6\u9519\u8bef\u5bfc\u81f4\u7a0b\u5e8f\u4e2d\u65ad\uff0c\u63d0\u5347\u7a0b\u5e8f\u5065\u58ee\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u901a\u8fc7\u5408\u7406\u7684\u9519\u8bef\u5904\u7406\uff0c\u80fd\u591f\u63d0\u5347\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002\u4ee5\u4e0b\u662f\u51e0\u6761\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6355\u83b7\u5177\u4f53\u5f02\u5e38<\/strong>\uff1a\u660e\u786e\u6355\u83b7\u7279\u5b9a\u5f02\u5e38\u7c7b\u578b\uff0c\u800c\u975e\u901a\u7528\u5f02\u5e38\u3002<\/li>\n\n\n\n<li><strong>\u591a\u91cd\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u9488\u5bf9\u4e0d\u540c\u7684\u5f02\u5e38\u7c7b\u578b\u5206\u522b\u8fdb\u884c\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong><code>else<\/code> \u548c <code>finally<\/code> \u7684\u914d\u5408\u4f7f\u7528<\/strong>\uff1a\u786e\u4fdd\u6e05\u7406\u5de5\u4f5c\u548c\u5f02\u5e38\u540e\u5904\u7406\u90fd\u5f97\u5230\u6709\u6548\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong>\uff1a\u901a\u8fc7\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u578b\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/li>\n\n\n\n<li><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u901a\u8fc7 <code>logging<\/code> \u6a21\u5757\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f\uff0c\u4ee5\u4fbf\u540e\u7eed\u6392\u67e5\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u5e38\u94fe<\/strong>\uff1a\u4f7f\u7528 <code>raise ... from<\/code> \u8bed\u6cd5\u4fdd\u7559\u5f02\u5e38\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5408\u7406\u7684\u9519\u8bef\u5904\u7406\uff0c\u7a0b\u5e8f\u5728\u9762\u5bf9\u610f\u5916\u60c5\u51b5\u65f6\u80fd\u591f\u4f18\u96c5\u5730\u964d\u7ea7\u5904\u7406\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5d29\u6e83\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u9519\u8bef\u5904\u7406\u662f\u7f16\u7a0b\u4e2d\u81f3\u5173\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u5408\u7406\u7684\u9519\u8bef\u5904\u7406\u80fd\u591f\u589e\u5f3a\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-934","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=934"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/934\/revisions"}],"predecessor-version":[{"id":935,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/934\/revisions\/935"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}