{"id":1163847,"date":"2025-01-15T14:59:49","date_gmt":"2025-01-15T06:59:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1163847.html"},"modified":"2025-01-15T14:59:52","modified_gmt":"2025-01-15T06:59:52","slug":"python%e8%af%ad%e5%8f%a5%e6%8a%a5%e9%94%99%e5%a6%82%e4%bd%95%e7%bb%a7%e7%bb%ad%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1163847.html","title":{"rendered":"python\u8bed\u53e5\u62a5\u9519\u5982\u4f55\u7ee7\u7eed\u6267\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25204514\/b94cb996-88c9-463f-bddf-2cb5805d09ce.webp\" alt=\"python\u8bed\u53e5\u62a5\u9519\u5982\u4f55\u7ee7\u7eed\u6267\u884c\" \/><\/p>\n<p><p> <strong>Python\u8bed\u53e5\u62a5\u9519\u7ee7\u7eed\u6267\u884c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528try-except\u5757\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u5904\u7406\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef\u3001\u5728\u51fd\u6570\u4e2d\u5904\u7406\u5f02\u5e38\u3001\u4f7f\u7528finally\u5757\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u6709\u6548\u7684\u65b9\u5f0f\u662f\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u7ec8\u6b62\uff0c\u800c\u662f\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>try-except\u5757\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<p>try-except\u5757\u662fPython\u4e2d\u7528\u4e8e\u5904\u7406\u5f02\u5e38\u7684\u4e3b\u8981\u673a\u5236\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>except \u5f02\u5e38\u7c7b\u578b:<\/p>\n<p>    # \u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728try\u5757\u4e2d\u653e\u7f6e\u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\u3002\u5982\u679c\u8be5\u4ee3\u7801\u5728\u6267\u884c\u65f6\u5f15\u53d1\u4e86\u6307\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u7acb\u5373\u8df3\u8f6c\u5230except\u5757\u4e2d\uff0c\u5e76\u6267\u884cexcept\u5757\u4e2d\u7684\u4ee3\u7801\u3002\u8fd9\u6837\u53ef\u4ee5\u9632\u6b62\u7a0b\u5e8f\u56e0\u672a\u5904\u7406\u7684\u5f02\u5e38\u800c\u5d29\u6e83\uff0c\u5e76\u5141\u8bb8\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    print(&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9664\u4ee5\u96f6\u4f1a\u5f15\u53d1ZeroDivisionError\u5f02\u5e38\uff0c\u4f46\u7531\u4e8e\u6211\u4eec\u5728except\u5757\u4e2d\u6355\u83b7\u5e76\u5904\u7406\u4e86\u8fd9\u4e2a\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u5d29\u6e83\uff0c\u800c\u662f\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528try-except\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>try-except\u5757\u662fPython\u4e2d\u5904\u7406\u5f02\u5e38\u7684\u6700\u57fa\u672c\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\u653e\u5728try\u5757\u4e2d\uff0c\u5e76\u5728except\u5757\u4e2d\u5904\u7406\u8be5\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError as e:<\/p>\n<p>    # \u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2a\u5f02\u5e38: {e}&quot;)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c1d\u8bd5\u9664\u4ee5\u96f6\uff0c\u8fd9\u4f1a\u5f15\u53d1ZeroDivisionError\u5f02\u5e38\u3002\u7531\u4e8e\u6211\u4eec\u5728except\u5757\u4e2d\u6355\u83b7\u5e76\u5904\u7406\u4e86\u8fd9\u4e2a\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u7ec8\u6b62\uff0c\u800c\u662f\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6355\u83b7\u591a\u4e2a\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6355\u83b7\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2atry\u5757\u4e2d\u4f7f\u7528\u591a\u4e2aexcept\u5757\u6765\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>    number = int(&quot;abc&quot;)<\/p>\n<p>except ZeroDivisionError as e:<\/p>\n<p>    # \u5904\u7406\u9664\u4ee5\u96f6\u5f02\u5e38<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aZeroDivisionError: {e}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    # \u5904\u7406\u503c\u9519\u8bef\u5f02\u5e38<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aValueError: {e}&quot;)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c1d\u8bd5\u9664\u4ee5\u96f6\u548c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u8fd9\u4e24\u8005\u90fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u6211\u4eec\u4f7f\u7528\u591a\u4e2aexcept\u5757\u5206\u522b\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4efb\u4f55\u4e00\u4e2a\u5f02\u5e38\u800c\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6355\u83b7\u6240\u6709\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u6355\u83b7\u6240\u6709\u53ef\u80fd\u7684\u5f02\u5e38\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728except\u5757\u4e2d\u4e0d\u6307\u5b9a\u5f02\u5e38\u7c7b\u578b\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    # \u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except Exception as e:<\/p>\n<p>    # \u5904\u7406\u6240\u6709\u7c7b\u578b\u7684\u5f02\u5e38<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2a\u5f02\u5e38: {e}&quot;)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u901a\u7528\u7684Exception\u7c7b\u578b\u6765\u6355\u83b7\u6240\u6709\u53ef\u80fd\u7684\u5f02\u5e38\u3002\u8fd9\u867d\u7136\u786e\u4fdd\u4e86\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u4f46\u4e5f\u53ef\u80fd\u63a9\u76d6\u4e00\u4e9b\u4e0d\u671f\u671b\u6355\u83b7\u7684\u5f02\u5e38\uff0c\u56e0\u6b64\u5e94\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u5904\u7406<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u662f\u5904\u7406\u5e76\u53d1\u4efb\u52a1\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u901a\u8fc7\u5c06\u4efb\u52a1\u5206\u914d\u5230\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u5373\u4f7f\u67d0\u4e2a\u7ebf\u7a0b\u4e2d\u7684\u4efb\u52a1\u5f15\u53d1\u4e86\u5f02\u5e38\uff0c\u4e5f\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u7ebf\u7a0b\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>threading.Thread<\/code>\u7c7b\u6765\u521b\u5efa\u7ebf\u7a0b\uff0c\u5e76\u5c06\u8981\u6267\u884c\u7684\u4efb\u52a1\u4f20\u9012\u7ed9\u5b83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def task():<\/p>\n<p>    try:<\/p>\n<p>        result = 10 \/ 0<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aZeroDivisionError: {e}&quot;)<\/p>\n<p>thread = threading.Thread(target=task)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u5e76\u5c06\u4efb\u52a1\u5206\u914d\u7ed9\u5b83\u3002\u4efb\u52a1\u4e2d\u5c1d\u8bd5\u9664\u4ee5\u96f6\u4f1a\u5f15\u53d1\u5f02\u5e38\uff0c\u4f46\u7531\u4e8e\u6211\u4eec\u5728\u4efb\u52a1\u4e2d\u4f7f\u7528\u4e86try-except\u5757\u8fdb\u884c\u5904\u7406\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u7ebf\u7a0b\u4e2d\u7684\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5f53\u7ebf\u7a0b\u4e2d\u7684\u4efb\u52a1\u5f15\u53d1\u5f02\u5e38\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u4e3a\u6b64\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e2a\u7ebf\u7a0b\u7c7b\uff0c\u5e76\u91cd\u5199<code>run<\/code>\u65b9\u6cd5\u6765\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>class <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>Thread(threading.Thread):<\/p>\n<p>    def run(self):<\/p>\n<p>        try:<\/p>\n<p>            # \u4efb\u52a1\u4ee3\u7801<\/p>\n<p>            result = 10 \/ 0<\/p>\n<p>        except Exception as e:<\/p>\n<p>            print(f&quot;\u7ebf\u7a0b\u4e2d\u6355\u83b7\u5230\u4e00\u4e2a\u5f02\u5e38: {e}&quot;)<\/p>\n<p>thread = SafeThread()<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u81ea\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>SafeThread<\/code>\u7c7b\uff0c\u5e76\u5728<code>run<\/code>\u65b9\u6cd5\u4e2d\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u7ebf\u7a0b\u4e2d\u7684\u4efb\u52a1\u5f15\u53d1\u4e86\u5f02\u5e38\uff0c\u7a0b\u5e8f\u4e5f\u4e0d\u4f1a\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef\u662f\u4e00\u79cd\u6709\u6548\u7684\u9519\u8bef\u5904\u7406\u65b9\u6cd5\u3002\u901a\u8fc7\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u540e\u7eed\u5206\u6790\u548c\u8c03\u8bd5\u4e2d\u4e86\u89e3\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u51b5\u548c\u9519\u8bef\u539f\u56e0\u3002Python\u7684<code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u914d\u7f6e\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u6765\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\uff0c\u5305\u62ec\u65e5\u5fd7\u7ea7\u522b\u3001\u65e5\u5fd7\u683c\u5f0f\u548c\u65e5\u5fd7\u8f93\u51fa\u4f4d\u7f6e\u3002<\/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>try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError as e:<\/p>\n<p>    logging.error(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aZeroDivisionError: {e}&quot;)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u914d\u7f6e\u4e86\u65e5\u5fd7\u8bb0\u5f55\uff0c\u5e76\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u3002\u8fd9\u4e0d\u4ec5\u786e\u4fdd\u4e86\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\uff0c\u8fd8\u80fd\u5728\u65e5\u5fd7\u4e2d\u4fdd\u7559\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u4f9b\u540e\u7eed\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p><code>logging<\/code>\u6a21\u5757\u652f\u6301\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u8bb0\u5f55\uff0c\u5305\u62ecDEBUG\u3001INFO\u3001WARNING\u3001ERROR\u548cCRITICAL\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9519\u8bef\u7684\u4e25\u91cd\u7a0b\u5ea6\u9009\u62e9\u5408\u9002\u7684\u65e5\u5fd7\u7ea7\u522b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.DEBUG, 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\u4e00\u4e2aZeroDivisionError: {e}&quot;)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u65e5\u5fd7\u7ea7\u522b\u8bbe\u7f6e\u4e3aDEBUG\uff0c\u8fd9\u6837\u6240\u6709\u7ea7\u522b\u7684\u65e5\u5fd7\u90fd\u4f1a\u88ab\u8bb0\u5f55\u3002\u6211\u4eec\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7\uff0c\u786e\u4fdd\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u88ab\u8bb0\u5f55\u4e0b\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5728\u51fd\u6570\u4e2d\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u4e2d\u5904\u7406\u5f02\u5e38\u662f\u4e00\u79cd\u5c06\u9519\u8bef\u5904\u7406\u903b\u8f91\u5c01\u88c5\u5728\u51fd\u6570\u5185\u90e8\u7684\u65b9\u6cd5\u3002\u8fd9\u4f7f\u5f97\u51fd\u6570\u7684\u8c03\u7528\u8005\u4e0d\u9700\u8981\u5173\u5fc3\u51fd\u6570\u5185\u90e8\u7684\u9519\u8bef\u5904\u7406\u7ec6\u8282\uff0c\u53ea\u9700\u5904\u7406\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6216\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5c01\u88c5\u5f02\u5e38\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\uff0c\u5e76\u901a\u8fc7\u8fd4\u56de\u503c\u6216\u5f02\u5e38\u4fe1\u606f\u901a\u77e5\u8c03\u7528\u8005\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def safe_divide(a, b):<\/p>\n<p>    try:<\/p>\n<p>        return a \/ b<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aZeroDivisionError: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>result = safe_divide(10, 0)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>safe_divide<\/code>\u51fd\u6570\uff0c\u5e76\u5728\u51fd\u6570\u5185\u90e8\u5904\u7406\u9664\u4ee5\u96f6\u5f02\u5e38\u3002\u5f53\u8c03\u7528\u8005\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u4e0d\u9700\u8981\u5173\u5fc3\u9664\u4ee5\u96f6\u5f02\u5e38\u7684\u5904\u7406\u7ec6\u8282\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8fd4\u56de\u5f02\u5e38\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u51fd\u6570\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\u8fd4\u56de\u5f02\u5e38\u4fe1\u606f\uff0c\u4ee5\u4fbf\u8c03\u7528\u8005\u53ef\u4ee5\u6839\u636e\u5f02\u5e38\u4fe1\u606f\u91c7\u53d6\u4e0d\u540c\u7684\u5904\u7406\u63aa\u65bd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def safe_divide(a, b):<\/p>\n<p>    try:<\/p>\n<p>        return a \/ b<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        return f&quot;\u6355\u83b7\u5230\u4e00\u4e2aZeroDivisionError: {e}&quot;<\/p>\n<p>result = safe_divide(10, 0)<\/p>\n<p>print(result)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\u8fd4\u56de\u5f02\u5e38\u4fe1\u606f\u3002\u8fd9\u6837\uff0c\u8c03\u7528\u8005\u53ef\u4ee5\u6839\u636e\u8fd4\u56de\u7684\u5f02\u5e38\u4fe1\u606f\u8fdb\u884c\u5904\u7406\uff0c\u800c\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u5f02\u5e38\u800c\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528finally\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>finally\u5757\u662fPython\u4e2d\u4e0etry-except\u5757\u914d\u5408\u4f7f\u7528\u7684\u4e00\u90e8\u5206\u3002\u65e0\u8bba\u662f\u5426\u5f15\u53d1\u5f02\u5e38\uff0cfinally\u5757\u4e2d\u7684\u4ee3\u7801\u90fd\u4f1a\u6267\u884c\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u5728\u5f02\u5e38\u5904\u7406\u540e\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u7684\u573a\u666f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728try-except\u5757\u540e\u6dfb\u52a0finally\u5757\uff0c\u4ee5\u786e\u4fdd\u67d0\u4e9b\u4ee3\u7801\u5728\u5f02\u5e38\u5904\u7406\u540e\u603b\u662f\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError as e:<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aZeroDivisionError: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    print(&quot;\u6e05\u7406\u64cd\u4f5c&quot;)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u65e0\u8bba\u662f\u5426\u5f15\u53d1\u5f02\u5e38\uff0cfinally\u5757\u4e2d\u7684\u6e05\u7406\u64cd\u4f5c\u4ee3\u7801\u90fd\u4f1a\u6267\u884c\u3002\u8fd9\u786e\u4fdd\u4e86\u5fc5\u8981\u7684\u6e05\u7406\u64cd\u4f5c\u4e0d\u4f1a\u56e0\u4e3a\u5f02\u5e38\u800c\u88ab\u8df3\u8fc7\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u7ed3\u5408\u591a\u4e2a\u5f02\u5e38\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06finally\u5757\u4e0e\u591a\u4e2aexcept\u5757\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u786e\u4fdd\u5728\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u5f02\u5e38\u540e\u6267\u884c\u5fc5\u8981\u7684\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError as e:<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aZeroDivisionError: {e}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aValueError: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    print(&quot;\u6e05\u7406\u64cd\u4f5c&quot;)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u65e0\u8bba\u5f15\u53d1\u7684\u662f\u54ea\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\uff0cfinally\u5757\u4e2d\u7684\u6e05\u7406\u64cd\u4f5c\u90fd\u4f1a\u6267\u884c\u3002\u8fd9\u786e\u4fdd\u4e86\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684Python\u7ed3\u6784\uff0c\u901a\u5e38\u4e0e<code>with<\/code>\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\u3002\u5b83\u53ef\u4ee5\u786e\u4fdd\u8d44\u6e90\u5728\u4f7f\u7528\u540e\u88ab\u6b63\u786e\u91ca\u653e\uff0c\u5373\u4f7f\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u5f02\u5e38\u3002\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u901a\u8fc7\u5b9e\u73b0<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\u6765\u7ba1\u7406\u8d44\u6e90\u7684\u521d\u59cb\u5316\u548c\u6e05\u7406\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u548c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u7ba1\u7406\u6587\u4ef6\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u8d44\u6e90\uff0c\u786e\u4fdd\u5b83\u4eec\u5728\u4f7f\u7528\u540e\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    try:<\/p>\n<p>        content = file.read()<\/p>\n<p>        result = 10 \/ 0<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aZeroDivisionError: {e}&quot;)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u5728try-except\u5757\u4e2d\u5904\u7406\u5f02\u5e38\u3002\u5373\u4f7f\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u5f15\u53d1\u4e86\u5f02\u5e38\uff0c\u6587\u4ef6\u4e5f\u4f1a\u5728\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684\u63a7\u5236\u4e0b\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u7ba1\u7406\u5176\u4ed6\u8d44\u6e90\u3002\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u9700\u8981\u5b9e\u73b0<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CustomContextManager:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&quot;\u8d44\u6e90\u521d\u59cb\u5316&quot;)<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_val, exc_tb):<\/p>\n<p>        print(&quot;\u8d44\u6e90\u6e05\u7406&quot;)<\/p>\n<p>        if exc_type:<\/p>\n<p>            print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2a\u5f02\u5e38: {exc_val}&quot;)<\/p>\n<p>with CustomContextManager() as manager:<\/p>\n<p>    try:<\/p>\n<p>        result = 10 \/ 0<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aZeroDivisionError: {e}&quot;)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u81ea\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>CustomContextManager<\/code>\uff0c\u5e76\u5728\u5176\u4e2d\u5b9e\u73b0\u4e86\u8d44\u6e90\u7684\u521d\u59cb\u5316\u548c\u6e05\u7406\u3002\u5373\u4f7f\u5728\u4f7f\u7528\u8d44\u6e90\u65f6\u5f15\u53d1\u4e86\u5f02\u5e38\uff0c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4e5f\u4f1a\u786e\u4fdd\u8d44\u6e90\u88ab\u6b63\u786e\u6e05\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u88c5\u9970\u5668\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u884c\u4e3a\u7684\u9ad8\u7ea7Python\u7279\u6027\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6765\u5728\u51fd\u6570\u8c03\u7528\u524d\u540e\u6dfb\u52a0\u989d\u5916\u7684\u903b\u8f91\uff0c\u5305\u62ec\u5f02\u5e38\u5904\u7406\u3002\u901a\u8fc7\u4f7f\u7528\u88c5\u9970\u5668\u5904\u7406\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5f02\u5e38\u5904\u7406\u903b\u8f91\u4ece\u51fd\u6570\u4e2d\u5206\u79bb\u51fa\u6765\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u53ef\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u5728\u88c5\u9970\u5668\u5185\u90e8\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u51fd\u6570\u4e2d\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def exception_handler(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        try:<\/p>\n<p>            return func(*args, kwargs)<\/p>\n<p>        except Exception as e:<\/p>\n<p>            print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2a\u5f02\u5e38: {e}&quot;)<\/p>\n<p>    return wrapper<\/p>\n<p>@exception_handler<\/p>\n<p>def divide(a, b):<\/p>\n<p>    return a \/ b<\/p>\n<p>result = divide(10, 0)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u88c5\u9970\u5668<code>exception_handler<\/code>\uff0c\u5e76\u4f7f\u7528\u5b83\u88c5\u9970<code>divide<\/code>\u51fd\u6570\u3002\u8fd9\u6837\uff0c\u5f53\u8c03\u7528<code>divide<\/code>\u51fd\u6570\u65f6\uff0c\u5f02\u5e38\u5904\u7406\u903b\u8f91\u4f1a\u81ea\u52a8\u5e94\u7528\u4e8e\u51fd\u6570\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728\u88c5\u9970\u5668\u4e2d\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u5e76\u6839\u636e\u5f02\u5e38\u7c7b\u578b\u91c7\u53d6\u4e0d\u540c\u7684\u5904\u7406\u63aa\u65bd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def exception_handler(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        try:<\/p>\n<p>            return func(*args, kwargs)<\/p>\n<p>        except ZeroDivisionError as e:<\/p>\n<p>            print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aZeroDivisionError: {e}&quot;)<\/p>\n<p>        except ValueError as e:<\/p>\n<p>            print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2aValueError: {e}&quot;)<\/p>\n<p>    return wrapper<\/p>\n<p>@exception_handler<\/p>\n<p>def divide(a, b):<\/p>\n<p>    return a \/ b<\/p>\n<p>result = divide(10, 0)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u88c5\u9970\u5668\u4e2d\u5904\u7406\u4e86ZeroDivisionError\u548cValueError\u4e24\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u5e76\u5728\u6355\u83b7\u5230\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u65f6\u91c7\u53d6\u4e0d\u540c\u7684\u5904\u7406\u63aa\u65bd\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u4fe1\u53f7\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u4fe1\u53f7\u662f\u4e00\u79cd\u7528\u4e8e\u901a\u77e5\u8fdb\u7a0b\u67d0\u4e9b\u4e8b\u4ef6\u53d1\u751f\u7684\u673a\u5236\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>signal<\/code>\u6a21\u5757\u6765\u5904\u7406\u4fe1\u53f7\uff0c\u5305\u62ec\u6355\u83b7\u548c\u5904\u7406\u4fe1\u53f7\u5f15\u53d1\u7684\u5f02\u5e38\u3002\u901a\u8fc7\u5904\u7406\u4fe1\u53f7\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u63a5\u6536\u5230\u67d0\u4e9b\u4fe1\u53f7\u65f6\u4e0d\u4f1a\u7ec8\u6b62\uff0c\u800c\u662f\u6267\u884c\u7279\u5b9a\u7684\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6355\u83b7\u548c\u5904\u7406\u4fe1\u53f7<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>signal.signal<\/code>\u51fd\u6570\u6765\u6355\u83b7\u548c\u5904\u7406\u4fe1\u53f7\u3002\u5728\u6355\u83b7\u5230\u4fe1\u53f7\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u7279\u5b9a\u7684\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>import time<\/p>\n<p>def handle_signal(signum, frame):<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u4fe1\u53f7: {signum}&quot;)<\/p>\n<p>signal.signal(signal.SIGINT, handle_signal)<\/p>\n<p>print(&quot;\u6309\u4e0bCtrl+C\u53d1\u9001SIGINT\u4fe1\u53f7&quot;)<\/p>\n<p>time.sleep(10)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6355\u83b7\u4e86SIGINT\u4fe1\u53f7\uff08\u901a\u5e38\u7531Ctrl+C\u53d1\u9001\uff09\uff0c\u5e76\u5728\u6355\u83b7\u5230\u4fe1\u53f7\u65f6\u6267\u884c\u5904\u7406\u903b\u8f91\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u7528\u6237\u6309\u4e0bCtrl+C\uff0c\u7a0b\u5e8f\u4e5f\u4e0d\u4f1a\u7acb\u5373\u7ec8\u6b62\uff0c\u800c\u662f\u6267\u884c\u5904\u7406\u903b\u8f91\u540e\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u591a\u4e2a\u4fe1\u53f7<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u591a\u4e2a\u4fe1\u53f7\uff0c\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u4fe1\u53f7\u8bbe\u7f6e\u4e0d\u540c\u7684\u5904\u7406\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import signal<\/p>\n<p>import time<\/p>\n<p>def handle_sigint(signum, frame):<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230SIGINT\u4fe1\u53f7: {signum}&quot;)<\/p>\n<p>def handle_sigterm(signum, frame):<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230SIGTERM\u4fe1\u53f7: {signum}&quot;)<\/p>\n<p>signal.signal(signal.SIGINT, handle_sigint)<\/p>\n<p>signal.signal(signal.SIGTERM, handle_sigterm)<\/p>\n<p>print(&quot;\u6309\u4e0bCtrl+C\u53d1\u9001SIGINT\u4fe1\u53f7\uff0c\u6216\u53d1\u9001SIGTERM\u4fe1\u53f7&quot;)<\/p>\n<p>time.sleep(10)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u6355\u83b7\u4e86SIGINT\u548cSIGTERM\u4fe1\u53f7\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u4fe1\u53f7\u8bbe\u7f6e\u4e86\u4e0d\u540c\u7684\u5904\u7406\u51fd\u6570\u3002\u8fd9\u6837\uff0c\u5f53\u7a0b\u5e8f\u63a5\u6536\u5230\u4e0d\u540c\u7684\u4fe1\u53f7\u65f6\uff0c\u4f1a\u6267\u884c\u76f8\u5e94\u7684\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u5728\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u65f6\u81ea\u52a8\u8c03\u7528\u7684\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u5f02\u5e38\uff0c\u5f53\u5f02\u5e38\u53d1\u751f\u65f6\u81ea\u52a8\u8c03\u7528\u56de\u8c03\u51fd\u6570\u6267\u884c\u7279\u5b9a\u7684\u5904\u7406\u903b\u8f91\u3002\u901a\u8fc7\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u5904\u7406\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5f02\u5e38\u5904\u7406\u903b\u8f91\u4e0e\u4e1a\u52a1\u903b\u8f91\u5206\u79bb\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u53ef\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u56de\u8c03\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5728\u56de\u8c03\u51fd\u6570\u5185\u90e8\u5904\u7406\u5f02\u5e38\u903b\u8f91\u3002\u5f53\u5f02\u5e38\u53d1\u751f\u65f6\uff0c\u81ea\u52a8\u8c03\u7528\u56de\u8c03\u51fd\u6570\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def handle_exception(e):<\/p>\n<p>    print(f&quot;\u6355\u83b7\u5230\u4e00\u4e2a\u5f02\u5e38: {e}&quot;)<\/p>\n<p>def divide(a, b, callback):<\/p>\n<p>    try:<\/p>\n<p>        return a \/ b<\/p>\n<p>    except Exception as e:<\/p>\n<p>        callback(e)<\/p>\n<p>result = divide(10, 0, handle_exception)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u56de\u8c03\u51fd\u6570<code>handle_exception<\/code>\uff0c\u5e76\u5728<code>divide<\/code><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5f02\u5e38\u4ee5\u4fbf\u7a0b\u5e8f\u80fd\u591f\u7ee7\u7eed\u6267\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\uff0c\u4ece\u800c\u4f7f\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5b8c\u5168\u7ec8\u6b62\u3002\u5177\u4f53\u505a\u6cd5\u662f\u5c06\u53ef\u80fd\u5f15\u53d1\u9519\u8bef\u7684\u4ee3\u7801\u653e\u5165<code>try<\/code>\u5757\u4e2d\uff0c\u800c\u5728<code>except<\/code>\u5757\u4e2d\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    # \u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n    result = 10 \/ 0  # \u8fd9\u4f1a\u5f15\u53d1ZeroDivisionError\nexcept ZeroDivisionError:\n    print(&quot;\u6355\u83b7\u5230\u9664\u4ee5\u96f6\u7684\u5f02\u5e38\uff0c\u7a0b\u5e8f\u5c06\u7ee7\u7eed\u6267\u884c\u3002&quot;)\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5373\u4f7f\u53d1\u751f\u4e86\u9664\u4ee5\u96f6\u7684\u9519\u8bef\uff0c\u7a0b\u5e8f\u4ecd\u7136\u80fd\u591f\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u5e0c\u671b\u5728\u53d1\u751f\u9519\u8bef\u540e\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff0c\u8be5\u5982\u4f55\u505a\u5230\uff1f<\/strong><br \/>\u53ef\u4ee5\u5728<code>except<\/code>\u5757\u4e2d\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u6765\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\u3002\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u7a0b\u5e8f\u8fd0\u884c\u540e\u8fdb\u884c\u8c03\u8bd5\u548c\u5206\u6790\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import logging\n\nlogging.basicConfig(level=logging.ERROR)\n\ntry:\n    # \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n    result = 10 \/ 0\nexcept ZeroDivisionError as e:\n    logging.error(f&quot;\u53d1\u751f\u9519\u8bef: {e}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u9519\u8bef\u4fe1\u606f\u4f1a\u88ab\u8bb0\u5f55\u5230\u65e5\u5fd7\u4e2d\uff0c\u800c\u7a0b\u5e8f\u4f9d\u7136\u80fd\u591f\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u4ee3\u7801\u3002<\/p>\n<p><strong>\u5728\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u5373\u4f7f\u67d0\u4e9b\u64cd\u4f5c\u5931\u8d25\uff0c\u5176\u4ed6\u64cd\u4f5c\u4e5f\u80fd\u7ee7\u7eed\u8fdb\u884c\uff1f<\/strong><br \/>\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u64cd\u4f5c\u653e\u5728\u72ec\u7acb\u7684<code>try<\/code>\u548c<code>except<\/code>\u5757\u4e2d\uff0c\u8fd9\u6837\u5373\u4f7f\u67d0\u4e2a\u64cd\u4f5c\u5931\u8d25\uff0c\u5176\u4ed6\u64cd\u4f5c\u4f9d\u7136\u80fd\u591f\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">operations = [lambda: 10 \/ 2, lambda: 10 \/ 0, lambda: print(&quot;\u6210\u529f\u6267\u884c&quot;)]\n\nfor operation in operations:\n    try:\n        operation()\n    except Exception as e:\n        print(f&quot;\u64cd\u4f5c\u5931\u8d25: {e}&quot;)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5373\u4f7f\u67d0\u4e2a\u64cd\u4f5c\u5931\u8d25\uff0c\u5176\u4ed6\u64cd\u4f5c\u4ecd\u7136\u4f1a\u88ab\u6267\u884c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u6574\u4f53\u8fd0\u884c\u4e0d\u53d7\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bed\u53e5\u62a5\u9519\u7ee7\u7eed\u6267\u884c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528try-except\u5757\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u5904\u7406\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u9519\u8bef\u3001\u5728\u51fd\u6570\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1163855,"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\/1163847"}],"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=1163847"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163847\/revisions"}],"predecessor-version":[{"id":1163857,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163847\/revisions\/1163857"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1163855"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1163847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1163847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1163847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}