{"id":2787,"date":"2025-03-14T23:08:11","date_gmt":"2025-03-14T15:08:11","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2787"},"modified":"2025-03-14T23:08:11","modified_gmt":"2025-03-14T15:08:11","slug":"python3-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/14\/python3-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8\/","title":{"rendered":"Python3 \u9519\u8bef\u548c\u5f02\u5e38"},"content":{"rendered":"\n<p>\u5728 Python \u4e2d\uff0c\u9519\u8bef\u548c\u5f02\u5e38\u662f\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u60c5\u51b5\u3002\u5f02\u5e38\u662f\u6307\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u53d1\u751f\u4e86\u9884\u671f\u4e4b\u5916\u7684\u9519\u8bef\uff0c\u901a\u5e38\u8fd9\u4e9b\u9519\u8bef\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u7684\u4e2d\u65ad\u3002Python \u63d0\u4f9b\u4e86\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u53ef\u4ee5\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\uff0c\u907f\u514d\u6574\u4e2a\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u9519\u8bef\u7c7b\u578b<\/strong><\/h2>\n\n\n\n<p>\u5728 Python \u4e2d\uff0c\u9519\u8bef\u4e3b\u8981\u5206\u4e3a\u4e24\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bed\u6cd5\u9519\u8bef<\/strong>\uff1a\u5373\u7a0b\u5e8f\u7684\u8bed\u6cd5\u4e0d\u6b63\u786e\uff0c\u89e3\u91ca\u5668\u65e0\u6cd5\u7406\u89e3\u3002\u4f8b\u5982\uff0c\u62ec\u53f7\u4e0d\u5339\u914d\u6216\u7f3a\u5c11\u5192\u53f7\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u5e38\u9519\u8bef<\/strong>\uff1a\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u53d1\u751f\u7684\u95ee\u9898\uff0c\u4e0d\u662f\u8bed\u6cd5\u4e0a\u7684\u9519\u8bef\uff0c\u901a\u5e38\u662f\u7531\u4e8e\u903b\u8f91\u4e0a\u7684\u95ee\u9898\u5bfc\u81f4\u7684\uff0c\u4f8b\u5982\u9664\u4ee5\u96f6\u3001\u6587\u4ef6\u4e0d\u5b58\u5728\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5f02\u5e38\uff08Exception\uff09<\/strong><\/h2>\n\n\n\n<p>\u5f02\u5e38\u662f\u7a0b\u5e8f\u8fd0\u884c\u65f6\u53d1\u751f\u7684\u9519\u8bef\u3002\u5f53\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u4e2d\u65ad\u5e76\u8df3\u8f6c\u5230\u76f8\u5e94\u7684\u5f02\u5e38\u5904\u7406\u4ee3\u7801\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u3002Python \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5f02\u5e38\u7c7b\uff0c\u51e0\u4e4e\u6bcf\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u8bef\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5f02\u5e38\u7c7b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5e38\u89c1\u7684\u5f02\u5e38\u7c7b\u578b\uff1a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>ZeroDivisionError<\/code><\/strong>\uff1a\u9664\u96f6\u9519\u8bef<\/li>\n\n\n\n<li><strong><code>FileNotFoundError<\/code><\/strong>\uff1a\u6587\u4ef6\u672a\u627e\u5230\u9519\u8bef<\/li>\n\n\n\n<li><strong><code>ValueError<\/code><\/strong>\uff1a\u503c\u9519\u8bef\uff0c\u901a\u5e38\u662f\u7c7b\u578b\u9519\u8bef<\/li>\n\n\n\n<li><strong><code>IndexError<\/code><\/strong>\uff1a\u7d22\u5f15\u8d8a\u754c\u9519\u8bef<\/li>\n\n\n\n<li><strong><code>KeyError<\/code><\/strong>\uff1a\u5b57\u5178\u952e\u9519\u8bef<\/li>\n\n\n\n<li><strong><code>TypeError<\/code><\/strong>\uff1a\u7c7b\u578b\u9519\u8bef\uff0c\u901a\u5e38\u53d1\u751f\u5728\u4e0d\u517c\u5bb9\u7684\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u64cd\u4f5c\u65f6<\/li>\n\n\n\n<li><strong><code>NameError<\/code><\/strong>\uff1a\u53d8\u91cf\u6216\u51fd\u6570\u672a\u5b9a\u4e49\u9519\u8bef<\/li>\n\n\n\n<li><strong><code>AttributeError<\/code><\/strong>\uff1a\u5c5e\u6027\u9519\u8bef\uff0c\u901a\u5e38\u6307\u5411\u5bf9\u8c61\u6ca1\u6709\u8be5\u5c5e\u6027<\/li>\n\n\n\n<li><strong><code>IOError<\/code><\/strong>\uff1a\u8f93\u5165\u8f93\u51fa\u9519\u8bef<\/li>\n\n\n\n<li><strong><code>OverflowError<\/code><\/strong>\uff1a\u6570\u5b57\u8fc7\u5927\u9519\u8bef<\/li>\n\n\n\n<li><strong><code>ImportError<\/code><\/strong>\uff1a\u6a21\u5757\u5bfc\u5165\u9519\u8bef<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u5f02\u5e38\u5904\u7406\uff1atry-except \u8bed\u53e5<\/strong><\/h2>\n\n\n\n<p>Python \u4f7f\u7528 <code>try-except<\/code> \u8bed\u53e5\u6765\u5904\u7406\u5f02\u5e38\u3002\u5f53\u7a0b\u5e8f\u4e2d\u7684\u4ee3\u7801\u53d1\u751f\u5f02\u5e38\u65f6\uff0cPython \u4f1a\u8df3\u8f6c\u5230 <code>except<\/code> \u5757\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8bed\u6cd5\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    # \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\nexcept ExceptionType as e:\n    # \u5f02\u5e38\u5904\u7406\u4ee3\u7801\n    print(f\"\u53d1\u751f\u5f02\u5e38: {e}\")<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>try<\/code><\/strong>\uff1a\u5305\u542b\u53ef\u80fd\u53d1\u751f\u9519\u8bef\u7684\u4ee3\u7801\u5757\u3002<\/li>\n\n\n\n<li><strong><code>except<\/code><\/strong>\uff1a\u6355\u83b7\u5e76\u5904\u7406\u6307\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/li>\n\n\n\n<li><strong><code>as<\/code><\/strong>\uff1a\u5c06\u5f02\u5e38\u5bf9\u8c61\u7ed1\u5b9a\u5230\u6307\u5b9a\u53d8\u91cf\u4e0a\uff0c\u53ef\u4ee5\u5728\u5f02\u5e38\u5904\u7406\u5757\u5185\u4f7f\u7528\u8be5\u53d8\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    x = 10 \/ 0  # \u9664\u4ee5\u96f6\u4f1a\u5f15\u53d1 ZeroDivisionError\nexcept ZeroDivisionError as e:\n    print(f\"\u9519\u8bef: {e}\")  # \u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u591a\u4e2a <code>except<\/code> \u5757<\/strong><\/h2>\n\n\n\n<p>\u53ef\u4ee5\u5728\u4e00\u4e2a <code>try<\/code> \u8bed\u53e5\u5757\u540e\u9762\u8ddf\u591a\u4e2a <code>except<\/code> \u5757\u6765\u6355\u83b7\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8bed\u6cd5\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    # \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\nexcept TypeError:\n    # \u5904\u7406\u7c7b\u578b\u9519\u8bef\nexcept ZeroDivisionError:\n    # \u5904\u7406\u9664\u96f6\u9519\u8bef\nexcept Exception as e:\n    # \u6355\u83b7\u5176\u4ed6\u5f02\u5e38\n    print(f\"\u53d1\u751f\u4e86\u5176\u4ed6\u9519\u8bef: {e}\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    x = 10 \/ 0  # \u9664\u4ee5\u96f6\u9519\u8bef\nexcept ZeroDivisionError:\n    print(\"\u4e0d\u80fd\u9664\u4ee5\u96f6\uff01\")\nexcept TypeError:\n    print(\"\u53d1\u751f\u4e86\u7c7b\u578b\u9519\u8bef\uff01\")\nexcept Exception as e:\n    print(f\"\u53d1\u751f\u4e86\u5176\u4ed6\u9519\u8bef: {e}\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. <code>else<\/code> \u548c <code>finally<\/code> \u5b50\u53e5<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>else<\/code> \u5b50\u53e5<\/strong>\uff1a\u5982\u679c <code>try<\/code> \u8bed\u53e5\u5757\u4e2d\u7684\u4ee3\u7801\u6ca1\u6709\u53d1\u751f\u5f02\u5e38\uff0c\u90a3\u4e48\u6267\u884c <code>else<\/code> \u5757\u4e2d\u7684\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong><code>finally<\/code> \u5b50\u53e5<\/strong>\uff1a\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884c <code>finally<\/code> \u5757\u4e2d\u7684\u4ee3\u7801\u3002\u901a\u5e38\u7528\u4e8e\u6e05\u7406\u8d44\u6e90\uff08\u5982\u6587\u4ef6\u5173\u95ed\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8bed\u6cd5\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    # \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\nexcept ExceptionType as e:\n    # \u5f02\u5e38\u5904\u7406\u4ee3\u7801\nelse:\n    # \u5982\u679c\u6ca1\u6709\u5f02\u5e38\uff0c\u6267\u884c\u6b64\u90e8\u5206\u4ee3\u7801\nfinally:\n    # \u65e0\u8bba\u5982\u4f55\u90fd\u6267\u884c\u6b64\u90e8\u5206\u4ee3\u7801<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    x = 10 \/ 2  # \u4e0d\u4f1a\u53d1\u751f\u5f02\u5e38\nexcept ZeroDivisionError:\n    print(\"\u4e0d\u80fd\u9664\u4ee5\u96f6\uff01\")\nelse:\n    print(\"\u8ba1\u7b97\u6210\u529f\uff01\")  # \u5982\u679c\u6ca1\u6709\u5f02\u5e38\uff0c\u6267\u884c\u8fd9\u91cc\nfinally:\n    print(\"\u6e05\u7406\u8d44\u6e90\")  # \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u4f1a\u6267\u884c<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8ba1\u7b97\u6210\u529f\uff01\n\u6e05\u7406\u8d44\u6e90<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/h2>\n\n\n\n<p>Python \u5141\u8bb8\u7528\u6237\u521b\u5efa\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\uff0c\u901a\u8fc7\u7ee7\u627f\u5185\u7f6e\u7684 <code>Exception<\/code> \u7c7b\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8bed\u6cd5\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyException(Exception):\n    pass\n\n# \u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\ntry:\n    raise MyException(\"\u8fd9\u662f\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\")\nexcept MyException as e:\n    print(f\"\u6355\u83b7\u81ea\u5b9a\u4e49\u5f02\u5e38: {e}\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class NegativeValueError(Exception):\n    pass\n\ndef check_value(x):\n    if x &lt; 0:\n        raise NegativeValueError(\"\u8f93\u5165\u7684\u503c\u4e0d\u80fd\u4e3a\u8d1f\u6570\")\n    return x\n\ntry:\n    print(check_value(-5))  # \u4f1a\u89e6\u53d1\u81ea\u5b9a\u4e49\u5f02\u5e38\nexcept NegativeValueError as e:\n    print(f\"\u9519\u8bef: {e}\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u629b\u51fa\u5f02\u5e38\uff08<code>raise<\/code>\uff09<\/strong><\/h2>\n\n\n\n<p>\u4f7f\u7528 <code>raise<\/code> \u8bed\u53e5\u53ef\u4ee5\u663e\u5f0f\u5730\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8bed\u6cd5\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>raise ExceptionType(\"\u5f02\u5e38\u4fe1\u606f\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def check_value(x):\n    if x &lt; 0:\n        raise ValueError(\"\u8f93\u5165\u7684\u503c\u4e0d\u80fd\u4e3a\u8d1f\u6570\")\n    return x\n\ntry:\n    print(check_value(-5))  # \u4f1a\u629b\u51fa ValueError\nexcept ValueError as e:\n    print(f\"\u9519\u8bef: {e}\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u83b7\u53d6\u5f02\u5e38\u4fe1\u606f\uff08<code>as<\/code>\uff09<\/strong><\/h2>\n\n\n\n<p>\u901a\u8fc7 <code>as<\/code> \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5c06\u5f02\u5e38\u5b9e\u4f8b\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u8fdb\u800c\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>try:\n    x = 10 \/ 0\nexcept ZeroDivisionError as e:\n    print(f\"\u53d1\u751f\u5f02\u5e38: {e}\")  # \u6355\u83b7\u5f02\u5e38\u4fe1\u606f\u5e76\u6253\u5370<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>Python \u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u975e\u5e38\u5f3a\u5927\uff0c\u5141\u8bb8\u7a0b\u5e8f\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5d29\u6e83\u3002\u901a\u8fc7 <code>try-except<\/code> \u8bed\u53e5\uff0c\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u3002\u540c\u65f6\uff0c<code>else<\/code> \u548c <code>finally<\/code> \u5b50\u53e5\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u63a7\u5236\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u9519\u8bef\u5904\u7406\u573a\u666f\u3002\u5bf9\u4e8e\u5f00\u53d1\u8005\u800c\u8a00\uff0c\u5408\u7406\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6709\u52a9\u4e8e\u7f16\u5199\u66f4\u52a0\u5065\u58ee\u7684\u4ee3\u7801\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u9519\u8bef\u548c\u5f02\u5e38\u662f\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u60c5\u51b5\u3002\u5f02\u5e38\u662f\u6307\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u53d1\u751f\u4e86\u9884\u671f\u4e4b\u5916\u7684\u9519\u8bef\uff0c\u901a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2788,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2787","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2787","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=2787"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2787\/revisions"}],"predecessor-version":[{"id":2789,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2787\/revisions\/2789"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2788"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}