{"id":10021229,"date":"2026-02-01T02:40:12","date_gmt":"2026-01-31T18:40:12","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021229"},"modified":"2026-02-01T02:40:12","modified_gmt":"2026-01-31T18:40:12","slug":"python%e4%b8%ad%e7%9a%84%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e6%9c%ba%e5%88%b6%e4%b8%8e%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021229","title":{"rendered":"Python\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u4e0e\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<h2>Python\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<h3>1. \u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u5f02\u5e38\u662f\u6307\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\u3002\u7406\u89e3\u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5\u662f\u638c\u63e1Python\u5f02\u5e38\u5904\u7406\u673a\u5236\u7684\u57fa\u7840\u3002<\/p>\n<h4>1.1 \u5f02\u5e38\u7684\u5b9a\u4e49<\/h4>\n<p>\u5f02\u5e38\u662f\u6307\u7a0b\u5e8f\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u975e\u9884\u671f\u60c5\u51b5\uff0c\u8fd9\u4e9b\u60c5\u51b5\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u7ee7\u7eed\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u9664\u4ee5\u96f6<\/li>\n<li>\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u6587\u4ef6<\/li>\n<li>\u7c7b\u578b\u9519\u8bef<\/li>\n<li>\u7d22\u5f15\u8d8a\u754c<\/li>\n<\/ul>\n<h4>1.2 \u5f02\u5e38\u4e0e\u9519\u8bef\u7684\u533a\u522b<\/h4>\n<p>\u5728Python\u4e2d\uff0c\u5f02\u5e38\u548c\u9519\u8bef\u6709\u4ee5\u4e0b\u533a\u522b\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u9519\u8bef<\/strong>\uff1a\u901a\u5e38\u6307\u8bed\u6cd5\u9519\u8bef\u6216\u903b\u8f91\u9519\u8bef\uff0c\u8fd9\u4e9b\u9519\u8bef\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c<\/p>\n<ul>\n<li>\u8bed\u6cd5\u9519\u8bef\uff1a\u4ee3\u7801\u4e0d\u7b26\u5408Python\u8bed\u6cd5\u89c4\u5219<\/li>\n<li>\u903b\u8f91\u9519\u8bef\uff1a\u4ee3\u7801\u903b\u8f91\u4e0d\u6b63\u786e\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u884c\u4e3a\u4e0d\u7b26\u5408\u9884\u671f<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5f02\u5e38<\/strong>\uff1a\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u975e\u9884\u671f\u60c5\u51b5\uff0c\u8fd9\u4e9b\u60c5\u51b5\u53ef\u4ee5\u88ab\u6355\u83b7\u548c\u5904\u7406<\/p>\n<ul>\n<li>\u8fd0\u884c\u65f6\u5f02\u5e38\uff1a\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u5f02\u5e38<\/li>\n<li>\u68c0\u67e5\u5f02\u5e38\uff1a\u9700\u8981\u663e\u5f0f\u5904\u7406\u7684\u5f02\u5e38\uff08\u5728Python\u4e2d\uff0c\u6240\u6709\u5f02\u5e38\u90fd\u662f\u8fd0\u884c\u65f6\u5f02\u5e38\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>1.3 \u5f02\u5e38\u7684\u5c42\u6b21\u7ed3\u6784<\/h4>\n<p>Python\u4e2d\u7684\u5f02\u5e38\u662f\u901a\u8fc7\u7c7b\u5c42\u6b21\u7ed3\u6784\u6765\u7ec4\u7ec7\u7684\uff0c\u6240\u6709\u5f02\u5e38\u7c7b\u90fd\u7ee7\u627f\u81ea<code>BaseException<\/code>\u7c7b\u3002\u5e38\u89c1\u7684\u5f02\u5e38\u7c7b\u5c42\u6b21\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre><code>BaseException\n\u251c\u2500\u2500 SystemExit\n\u251c\u2500\u2500 KeyboardInterrupt\n\u251c\u2500\u2500 GeneratorExit\n\u2514\u2500\u2500 Exception\n    \u251c\u2500\u2500 StopIteration\n    \u251c\u2500\u2500 StopAsyncIteration\n    \u251c\u2500\u2500 ArithmeticError\n    \u2502   \u251c\u2500\u2500 FloatingPointError\n    \u2502   \u251c\u2500\u2500 OverflowError\n    \u2502   \u2514\u2500\u2500 ZeroDivisionError\n    \u251c\u2500\u2500 AssertionError\n    \u251c\u2500\u2500 AttributeError\n    \u251c\u2500\u2500 BufferError\n    \u251c\u2500\u2500 EOFError\n    \u251c\u2500\u2500 ImportError\n    \u2502   \u2514\u2500\u2500 ModuleNotFoundError\n    \u251c\u2500\u2500 LookupError\n    \u2502   \u251c\u2500\u2500 IndexError\n    \u2502   \u2514\u2500\u2500 KeyError\n    \u251c\u2500\u2500 MemoryError\n    \u251c\u2500\u2500 NameError\n    \u2502   \u2514\u2500\u2500 UnboundLocalError\n    \u251c\u2500\u2500 OSError\n    \u2502   \u251c\u2500\u2500 BlockingIOError\n    \u2502   \u251c\u2500\u2500 ChildProcessError\n    \u2502   \u251c\u2500\u2500 ConnectionError\n    \u2502   \u2502   \u251c\u2500\u2500 BrokenPipeError\n    \u2502   \u2502   \u251c\u2500\u2500 ConnectionAbortedError\n    \u2502   \u2502   \u251c\u2500\u2500 ConnectionRefusedError\n    \u2502   \u2502   \u2514\u2500\u2500 ConnectionResetError\n    \u2502   \u251c\u2500\u2500 FileExistsError\n    \u2502   \u251c\u2500\u2500 FileNotFoundError\n    \u2502   \u251c\u2500\u2500 InterruptedError\n    \u2502   \u251c\u2500\u2500 IsADirectoryError\n    \u2502   \u251c\u2500\u2500 NotADirectoryError\n    \u2502   \u251c\u2500\u2500 PermissionError\n    \u2502   \u251c\u2500\u2500 ProcessLookupError\n    \u2502   \u2514\u2500\u2500 TimeoutError\n    \u251c\u2500\u2500 ReferenceError\n    \u251c\u2500\u2500 RuntimeError\n    \u2502   \u251c\u2500\u2500 NotImplementedError\n    \u2502   \u2514\u2500\u2500 RecursionError\n    \u251c\u2500\u2500 SyntaxError\n    \u2502   \u2514\u2500\u2500 IndentationError\n    \u2502       \u2514\u2500\u2500 TabError\n    \u251c\u2500\u2500 SystemError\n    \u251c\u2500\u2500 TypeError\n    \u251c\u2500\u2500 ValueError\n    \u2502   \u2514\u2500\u2500 UnicodeError\n    \u2502       \u251c\u2500\u2500 UnicodeDecodeError\n    \u2502       \u251c\u2500\u2500 UnicodeEncodeError\n    \u2502       \u2514\u2500\u2500 UnicodeTranslateError\n    \u2514\u2500\u2500 Warning\n        \u251c\u2500\u2500 DeprecationWarning\n        \u251c\u2500\u2500 PendingDeprecationWarning\n        \u251c\u2500\u2500 RuntimeWarning\n        \u251c\u2500\u2500 SyntaxWarning\n        \u251c\u2500\u2500 UserWarning\n        \u251c\u2500\u2500 FutureWarning\n        \u251c\u2500\u2500 ImportWarning\n        \u251c\u2500\u2500 UnicodeWarning\n        \u2514\u2500\u2500 BytesWarning<\/code><\/pre>\n<h4>1.4 \u5e38\u89c1\u7684\u5185\u7f6e\u5f02\u5e38<\/h4>\n<p>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u5f02\u5e38\uff0c\u7528\u4e8e\u8868\u793a\u4e0d\u540c\u7c7b\u578b\u7684\u9519\u8bef\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5185\u7f6e\u5f02\u5e38\uff1a<\/p>\n<ul>\n<li><strong>ZeroDivisionError<\/strong>\uff1a\u9664\u4ee5\u96f6<\/li>\n<li><strong>FileNotFoundError<\/strong>\uff1a\u6587\u4ef6\u4e0d\u5b58\u5728<\/li>\n<li><strong>TypeError<\/strong>\uff1a\u7c7b\u578b\u9519\u8bef<\/li>\n<li><strong>ValueError<\/strong>\uff1a\u503c\u9519\u8bef<\/li>\n<li><strong>IndexError<\/strong>\uff1a\u7d22\u5f15\u8d8a\u754c<\/li>\n<li><strong>KeyError<\/strong>\uff1a\u952e\u4e0d\u5b58\u5728<\/li>\n<li><strong>NameError<\/strong>\uff1a\u540d\u79f0\u9519\u8bef<\/li>\n<li><strong>AttributeError<\/strong>\uff1a\u5c5e\u6027\u9519\u8bef<\/li>\n<li><strong>ImportError<\/strong>\uff1a\u5bfc\u5165\u9519\u8bef<\/li>\n<li><strong>RuntimeError<\/strong>\uff1a\u8fd0\u884c\u65f6\u9519\u8bef<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5\u793a\u4f8b\n\n# 1. \u89e6\u53d1\u5e38\u89c1\u5f02\u5e38\nprint(&quot;\u89e6\u53d1\u5e38\u89c1\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\n# \u9664\u4ee5\u96f6 - ZeroDivisionError\ntry:\n    result = 10 \/ 0\nexcept ZeroDivisionError as e:\n    print(f&quot;ZeroDivisionError: {e}&quot;)\n\n# \u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u6587\u4ef6 - FileNotFoundError\ntry:\n    with open(&quot;non_existent_file.txt&quot;, &quot;r&quot;) as f:\n        content = f.read()\nexcept FileNotFoundError as e:\n    print(f&quot;FileNotFoundError: {e}&quot;)\n\n# \u7c7b\u578b\u9519\u8bef - TypeError\ntry:\n    result = &quot;10&quot; + 5\nexcept TypeError as e:\n    print(f&quot;TypeError: {e}&quot;)\n\n# \u503c\u9519\u8bef - ValueError\ntry:\n    result = int(&quot;abc&quot;)\nexcept ValueError as e:\n    print(f&quot;ValueError: {e}&quot;)\n\n# \u7d22\u5f15\u8d8a\u754c - IndexError\ntry:\n    lst = [1, 2, 3]\n    print(lst[5])\nexcept IndexError as e:\n    print(f&quot;IndexError: {e}&quot;)\n\n# \u952e\u4e0d\u5b58\u5728 - KeyError\ntry:\n    dct = {&quot;a&quot;: 1, &quot;b&quot;: 2}\n    print(dct[&quot;c&quot;])\nexcept KeyError as e:\n    print(f&quot;KeyError: {e}&quot;)\n\n# \u540d\u79f0\u9519\u8bef - NameError\ntry:\n    print(undefined_variable)\nexcept NameError as e:\n    print(f&quot;NameError: {e}&quot;)\n\n# \u5c5e\u6027\u9519\u8bef - AttributeError\ntry:\n    lst = [1, 2, 3]\n    lst.non_existent_method()\nexcept AttributeError as e:\n    print(f&quot;AttributeError: {e}&quot;)\n\n# 2. \u5f02\u5e38\u5c42\u6b21\u7ed3\u6784\nprint(&quot;\/n\u5f02\u5e38\u5c42\u6b21\u7ed3\u6784\u793a\u4f8b\uff1a&quot;)\n\n# \u67e5\u770b\u5f02\u5e38\u7684\u57fa\u7c7b\nprint(f&quot;ZeroDivisionError\u7684\u57fa\u7c7b: {ZeroDivisionError.__bases__}&quot;)\nprint(f&quot;ArithmeticError\u7684\u57fa\u7c7b: {ArithmeticError.__bases__}&quot;)\nprint(f&quot;Exception\u7684\u57fa\u7c7b: {Exception.__bases__}&quot;)\nprint(f&quot;BaseException\u7684\u57fa\u7c7b: {BaseException.__bases__}&quot;)\n\n# 3. \u6355\u83b7\u6240\u6709\u5f02\u5e38\nprint(&quot;\/n\u6355\u83b7\u6240\u6709\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\ntry:\n    result = 10 \/ 0\nexcept Exception as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {type(e).__name__}: {e}&quot;)\n\n# 4. \u6355\u83b7\u591a\u4e2a\u5f02\u5e38\ntry:\n    lst = [1, 2, 3]\n    print(lst[5])\nexcept (IndexError, ZeroDivisionError) as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {type(e).__name__}: {e}&quot;)\n\n# 5. \u5f02\u5e38\u7684\u4f20\u9012\ndef function1():\n    print(&quot;function1 \u5f00\u59cb&quot;)\n    function2()\n    print(&quot;function1 \u7ed3\u675f&quot;)\n\ndef function2():\n    print(&quot;function2 \u5f00\u59cb&quot;)\n    10 \/ 0\n    print(&quot;function2 \u7ed3\u675f&quot;)\n\nprint(&quot;\/n\u5f02\u5e38\u7684\u4f20\u9012\u793a\u4f8b\uff1a&quot;)\ntry:\n    function1()\nexcept ZeroDivisionError as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)<\/code><\/pre>\n<h3>2. \u5f02\u5e38\u5904\u7406\u673a\u5236<\/h3>\n<p>Python\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u5141\u8bb8\u6211\u4eec\u6355\u83b7\u548c\u5904\u7406\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u4ece\u800c\u4f7f\u7a0b\u5e8f\u80fd\u591f\u66f4\u52a0\u5065\u58ee\u548c\u53ef\u9760\u3002<\/p>\n<h4>2.1 \u5f02\u5e38\u5904\u7406\u7684\u57fa\u672c\u8bed\u6cd5<\/h4>\n<p>Python\u7684\u5f02\u5e38\u5904\u7406\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u5b9e\u73b0\uff0c\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"python\">try:\n    # \u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n    pass\nexcept ExceptionType1:\n    # \u5904\u7406ExceptionType1\u7c7b\u578b\u7684\u5f02\u5e38\n    pass\nexcept ExceptionType2:\n    # \u5904\u7406ExceptionType2\u7c7b\u578b\u7684\u5f02\u5e38\n    pass\nelse:\n    # \u5982\u679c\u6ca1\u6709\u5f15\u53d1\u5f02\u5e38\uff0c\u6267\u884c\u8fd9\u91cc\u7684\u4ee3\u7801\n    pass\nfinally:\n    # \u65e0\u8bba\u662f\u5426\u5f15\u53d1\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884c\u8fd9\u91cc\u7684\u4ee3\u7801\n    pass<\/code><\/pre>\n<h4>2.2 try\u5b50\u53e5<\/h4>\n<p><code>try<\/code>\u5b50\u53e5\u5305\u542b\u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\u3002\u5f53<code>try<\/code>\u5b50\u53e5\u4e2d\u7684\u4ee3\u7801\u6267\u884c\u65f6\uff0c\u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0cPython\u4f1a\u7acb\u5373\u505c\u6b62\u6267\u884c<code>try<\/code>\u5b50\u53e5\u4e2d\u7684\u5269\u4f59\u4ee3\u7801\uff0c\u5e76\u8df3\u8f6c\u5230\u76f8\u5e94\u7684<code>except<\/code>\u5b50\u53e5\u3002<\/p>\n<h4>2.3 except\u5b50\u53e5<\/h4>\n<p><code>except<\/code>\u5b50\u53e5\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u4e00\u4e2a<code>try<\/code>\u8bed\u53e5\u53ef\u4ee5\u5305\u542b\u591a\u4e2a<code>except<\/code>\u5b50\u53e5\uff0c\u6bcf\u4e2a<code>except<\/code>\u5b50\u53e5\u5904\u7406\u4e00\u79cd\u6216\u591a\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/p>\n<h4>2.4 else\u5b50\u53e5<\/h4>\n<p><code>else<\/code>\u5b50\u53e5\u662f\u53ef\u9009\u7684\uff0c\u5b83\u5305\u542b\u5f53<code>try<\/code>\u5b50\u53e5\u4e2d\u6ca1\u6709\u5f15\u53d1\u5f02\u5e38\u65f6\u6267\u884c\u7684\u4ee3\u7801\u3002<code>else<\/code>\u5b50\u53e5\u5fc5\u987b\u4f4d\u4e8e\u6240\u6709<code>except<\/code>\u5b50\u53e5\u4e4b\u540e\u3002<\/p>\n<h4>2.5 finally\u5b50\u53e5<\/h4>\n<p><code>finally<\/code>\u5b50\u53e5\u662f\u53ef\u9009\u7684\uff0c\u5b83\u5305\u542b\u65e0\u8bba\u662f\u5426\u5f15\u53d1\u5f02\u5e38\u90fd\u4f1a\u6267\u884c\u7684\u4ee3\u7801\u3002<code>finally<\/code>\u5b50\u53e5\u901a\u5e38\u7528\u4e8e\u91ca\u653e\u8d44\u6e90\uff0c\u4f8b\u5982\u5173\u95ed\u6587\u4ef6\u6216\u7f51\u7edc\u8fde\u63a5\u3002<\/p>\n<h4>2.6 \u5f02\u5e38\u5904\u7406\u7684\u6267\u884c\u6d41\u7a0b<\/h4>\n<p>\u5f02\u5e38\u5904\u7406\u7684\u6267\u884c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u6267\u884c<code>try<\/code>\u5b50\u53e5\u4e2d\u7684\u4ee3\u7801<\/li>\n<li>\u5982\u679c\u53d1\u751f\u5f02\u5e38\uff1a<br \/>a. \u505c\u6b62\u6267\u884c<code>try<\/code>\u5b50\u53e5\u4e2d\u7684\u5269\u4f59\u4ee3\u7801<br \/>b. \u67e5\u627e\u5339\u914d\u7684<code>except<\/code>\u5b50\u53e5<br \/>c. \u5982\u679c\u627e\u5230\u5339\u914d\u7684<code>except<\/code>\u5b50\u53e5\uff0c\u6267\u884c\u8be5\u5b50\u53e5\u4e2d\u7684\u4ee3\u7801<br \/>d. \u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684<code>except<\/code>\u5b50\u53e5\uff0c\u5f02\u5e38\u4f1a\u5411\u4e0a\u4f20\u9012<br \/>e. \u6267\u884c<code>finally<\/code>\u5b50\u53e5\u4e2d\u7684\u4ee3\u7801<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u53d1\u751f\u5f02\u5e38\uff1a<br \/>a. \u6267\u884c<code>else<\/code>\u5b50\u53e5\u4e2d\u7684\u4ee3\u7801<br \/>b. \u6267\u884c<code>finally<\/code>\u5b50\u53e5\u4e2d\u7684\u4ee3\u7801<\/li>\n<\/ol>\n<pre><code class=\"python\"># \u5f02\u5e38\u5904\u7406\u673a\u5236\u793a\u4f8b\n\n# 1. \u57fa\u672c\u7684try-except\u8bed\u53e5\nprint(&quot;\u57fa\u672c\u7684try-except\u8bed\u53e5\u793a\u4f8b\uff1a&quot;)\n\ntry:\n    num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))\n    result = 10 \/ num\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u9519\u8bef\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570&quot;)\nexcept ZeroDivisionError:\n    print(&quot;\u9519\u8bef\uff1a\u4e0d\u80fd\u9664\u4ee5\u96f6&quot;)\n\n# 2. \u5e26\u6709else\u5b50\u53e5\u7684try-except\u8bed\u53e5\nprint(&quot;\/n\u5e26\u6709else\u5b50\u53e5\u7684try-except\u8bed\u53e5\u793a\u4f8b\uff1a&quot;)\n\ntry:\n    num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))\n    result = 10 \/ num\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u9519\u8bef\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570&quot;)\nexcept ZeroDivisionError:\n    print(&quot;\u9519\u8bef\uff1a\u4e0d\u80fd\u9664\u4ee5\u96f6&quot;)\nelse:\n    print(f&quot;\u8ba1\u7b97\u6210\u529f\uff0c\u7ed3\u679c: {result}&quot;)\n\n# 3. \u5e26\u6709finally\u5b50\u53e5\u7684try-except\u8bed\u53e5\nprint(&quot;\/n\u5e26\u6709finally\u5b50\u53e5\u7684try-except\u8bed\u53e5\u793a\u4f8b\uff1a&quot;)\n\ntry:\n    print(&quot;\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6&quot;)\n    f = open(&quot;test.txt&quot;, &quot;w&quot;)\n    f.write(&quot;Hello, World!&quot;)\nexcept Exception as e:\n    print(f&quot;\u53d1\u751f\u5f02\u5e38: {e}&quot;)\nfinally:\n    print(&quot;\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884cfinally\u5b50\u53e5&quot;)\n    if 'f' in locals() and not f.closed:\n        print(&quot;\u5173\u95ed\u6587\u4ef6&quot;)\n        f.close()\n\n# 4. \u6355\u83b7\u6240\u6709\u5f02\u5e38\nprint(&quot;\/n\u6355\u83b7\u6240\u6709\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\ntry:\n    num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))\n    result = 10 \/ num\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nexcept Exception as e:\n    print(f&quot;\u53d1\u751f\u5f02\u5e38: {type(e).__name__}: {e}&quot;)\n\n# 5. \u5f02\u5e38\u7684\u4f20\u9012\nprint(&quot;\/n\u5f02\u5e38\u7684\u4f20\u9012\u793a\u4f8b\uff1a&quot;)\n\ndef read_file(filename):\n    with open(filename, &quot;r&quot;) as f:\n        content = f.read()\n    return content\n\ndef process_data(data):\n    lines = data.split('\/n')\n    return len(lines)\n\ndef main():\n    try:\n        data = read_file(&quot;non_existent_file.txt&quot;)\n        count = process_data(data)\n        print(f&quot;\u6587\u4ef6\u884c\u6570: {count}&quot;)\n    except FileNotFoundError as e:\n        print(f&quot;\u6587\u4ef6\u4e0d\u5b58\u5728: {e}&quot;)\n    except Exception as e:\n        print(f&quot;\u53d1\u751f\u5176\u4ed6\u5f02\u5e38: {e}&quot;)\n\nmain()\n\n# 6. \u5f02\u5e38\u5904\u7406\u7684\u5d4c\u5957\nprint(&quot;\/n\u5f02\u5e38\u5904\u7406\u7684\u5d4c\u5957\u793a\u4f8b\uff1a&quot;)\n\ntry:\n    print(&quot;\u5916\u5c42try&quot;)\n    try:\n        print(&quot;\u5185\u5c42try&quot;)\n        10 \/ 0\n    except ZeroDivisionError as e:\n        print(f&quot;\u5185\u5c42except: {e}&quot;)\n        # \u91cd\u65b0\u5f15\u53d1\u5f02\u5e38\n        raise\n    finally:\n        print(&quot;\u5185\u5c42finally&quot;)\nexcept Exception as e:\n    print(f&quot;\u5916\u5c42except: {e}&quot;)\nfinally:\n    print(&quot;\u5916\u5c42finally&quot;)<\/code><\/pre>\n<h3>3. \u5f02\u5e38\u7684\u5f15\u53d1\u4e0e\u4f20\u64ad<\/h3>\n<p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>raise<\/code>\u8bed\u53e5\u6765\u5f15\u53d1\u5f02\u5e38\uff0c\u4e5f\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\u540e\u91cd\u65b0\u5f15\u53d1\u5f02\u5e38\u3002\u7406\u89e3\u5f02\u5e38\u7684\u5f15\u53d1\u4e0e\u4f20\u64ad\u673a\u5236\u5bf9\u4e8e\u7f16\u5199\u5065\u58ee\u7684\u4ee3\u7801\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<h4>3.1 raise\u8bed\u53e5<\/h4>\n<p><code>raise<\/code>\u8bed\u53e5\u7528\u4e8e\u5f15\u53d1\u5f02\u5e38\uff0c\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"python\">raise ExceptionType(&quot;\u5f02\u5e38\u4fe1\u606f&quot;)<\/code><\/pre>\n<p><code>raise<\/code>\u8bed\u53e5\u53ef\u4ee5\u5f15\u53d1\u5185\u7f6e\u5f02\u5e38\u6216\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002\u5f53\u4f7f\u7528<code>raise<\/code>\u8bed\u53e5\u65f6\uff0cPython\u4f1a\u7acb\u5373\u505c\u6b62\u6267\u884c\u5f53\u524d\u4ee3\u7801\uff0c\u5e76\u5f00\u59cb\u67e5\u627e\u5339\u914d\u7684<code>except<\/code>\u5b50\u53e5\u3002<\/p>\n<h4>3.2 \u91cd\u65b0\u5f15\u53d1\u5f02\u5e38<\/h4>\n<p>\u5728<code>except<\/code>\u5b50\u53e5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u5e26\u53c2\u6570\u7684<code>raise<\/code>\u8bed\u53e5\u6765\u91cd\u65b0\u5f15\u53d1\u5f53\u524d\u6355\u83b7\u7684\u5f02\u5e38\u3002\u8fd9\u901a\u5e38\u7528\u4e8e\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f\u540e\uff0c\u5c06\u5f02\u5e38\u4f20\u9012\u7ed9\u4e0a\u5c42\u8c03\u7528\u8005\u5904\u7406\u3002<\/p>\n<h4>3.3 \u5f02\u5e38\u7684\u4f20\u64ad<\/h4>\n<p>\u5f53\u5f02\u5e38\u5728\u51fd\u6570\u6216\u65b9\u6cd5\u4e2d\u5f15\u53d1\u4f46\u672a\u88ab\u6355\u83b7\u65f6\uff0c\u5f02\u5e38\u4f1a\u5411\u4e0a\u4f20\u64ad\u5230\u8c03\u7528\u8be5\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u4ee3\u7801\u3002\u5982\u679c\u5f02\u5e38\u4e00\u76f4\u4f20\u64ad\u5230\u7a0b\u5e8f\u7684\u9876\u5c42\u800c\u672a\u88ab\u6355\u83b7\uff0c\u7a0b\u5e8f\u4f1a\u7ec8\u6b62\u5e76\u663e\u793a\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<h4>3.4 \u5f02\u5e38\u94fe<\/h4>\n<p>\u5728Python 3\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>raise NewException from OriginalException<\/code>\u8bed\u53e5\u6765\u521b\u5efa\u5f02\u5e38\u94fe\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u7559\u539f\u59cb\u5f02\u5e38\u7684\u4fe1\u606f\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u3002<\/p>\n<pre><code class=\"python\"># \u5f02\u5e38\u7684\u5f15\u53d1\u4e0e\u4f20\u64ad\u793a\u4f8b\n\n# 1. \u4f7f\u7528raise\u8bed\u53e5\u5f15\u53d1\u5f02\u5e38\nprint(&quot;\u4f7f\u7528raise\u8bed\u53e5\u5f15\u53d1\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\ndef divide(a, b):\n    if b == 0:\n        raise ZeroDivisionError(&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;)\n    return a \/ b\n\ntry:\n    result = divide(10, 0)\nexcept ZeroDivisionError as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n\n# 2. \u91cd\u65b0\u5f15\u53d1\u5f02\u5e38\nprint(&quot;\/n\u91cd\u65b0\u5f15\u53d1\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\ndef process_data(data):\n    try:\n        if not data:\n            raise ValueError(&quot;\u6570\u636e\u4e0d\u80fd\u4e3a\u7a7a&quot;)\n        return len(data)\n    except ValueError as e:\n        print(f&quot;\u8bb0\u5f55\u5f02\u5e38: {e}&quot;)\n        raise  # \u91cd\u65b0\u5f15\u53d1\u5f02\u5e38\n\ntry:\n    result = process_data(&quot;&quot;)\nexcept ValueError as e:\n    print(f&quot;\u6355\u83b7\u5230\u91cd\u65b0\u5f15\u53d1\u7684\u5f02\u5e38: {e}&quot;)\n\n# 3. \u5f02\u5e38\u7684\u4f20\u64ad\nprint(&quot;\/n\u5f02\u5e38\u7684\u4f20\u64ad\u793a\u4f8b\uff1a&quot;)\n\ndef level1():\n    print(&quot;level1 \u5f00\u59cb&quot;)\n    level2()\n    print(&quot;level1 \u7ed3\u675f&quot;)\n\ndef level2():\n    print(&quot;level2 \u5f00\u59cb&quot;)\n    level3()\n    print(&quot;level2 \u7ed3\u675f&quot;)\n\ndef level3():\n    print(&quot;level3 \u5f00\u59cb&quot;)\n    raise ValueError(&quot;\u5728level3\u4e2d\u5f15\u53d1\u5f02\u5e38&quot;)\n    print(&quot;level3 \u7ed3\u675f&quot;)\n\ntry:\n    level1()\nexcept ValueError as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n\n# 4. \u5f02\u5e38\u94fe\nprint(&quot;\/n\u5f02\u5e38\u94fe\u793a\u4f8b\uff1a&quot;)\n\ndef read_config():\n    try:\n        with open(&quot;config.json&quot;, &quot;r&quot;) as f:\n            # \u5047\u8bbe\u8fd9\u91cc\u9700\u8981\u89e3\u6790JSON\n            raise ValueError(&quot;JSON\u683c\u5f0f\u9519\u8bef&quot;)\n    except FileNotFoundError as e:\n        raise RuntimeError(&quot;\u65e0\u6cd5\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6&quot;) from e\n\ntry:\n    read_config()\nexcept RuntimeError as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n    if e.__cause__:\n        print(f&quot;\u539f\u59cb\u5f02\u5e38: {e.__cause__}&quot;)\n\n# 5. \u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\nprint(&quot;\/n\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u793a\u4f8b\uff1a&quot;)\n\nclass CustomError(Exception):\n    &quot;&quot;&quot;\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b&quot;&quot;&quot;\n    def __init__(self, message, error_code):\n        super().__init__(message)\n        self.error_code = error_code\n    \n    def __str__(self):\n        return f&quot;{self.__class__.__name__}: {self.args[0]} (\u9519\u8bef\u7801: {self.error_code})&quot;\n\ndef validate_input(value):\n    if value &lt; 0:\n        raise CustomError(&quot;\u8f93\u5165\u503c\u4e0d\u80fd\u4e3a\u8d1f\u6570&quot;, 400)\n    return value\n\ntry:\n    result = validate_input(-5)\nexcept CustomError as e:\n    print(f&quot;\u6355\u83b7\u5230\u81ea\u5b9a\u4e49\u5f02\u5e38: {e}&quot;)\n    print(f&quot;\u9519\u8bef\u7801: {e.error_code}&quot;)<\/code><\/pre>\n<h3>4. \u81ea\u5b9a\u4e49\u5f02\u5e38<\/h3>\n<p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u5185\u7f6e\u5f02\u5e38\u7c7b\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u60c5\u51b5\u3002<\/p>\n<h4>4.1 \u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<\/h4>\n<p>\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u7ee7\u627f\u4e00\u4e2a\u5185\u7f6e\u5f02\u5e38\u7c7b\uff08\u901a\u5e38\u662f<code>Exception<\/code>\u7c7b\uff09<\/li>\n<li>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/li>\n<li>\u5b9e\u73b0<code>__init__<\/code>\u65b9\u6cd5\uff08\u53ef\u9009\uff09<\/li>\n<li>\u5b9e\u73b0<code>__str__<\/code>\u65b9\u6cd5\uff08\u53ef\u9009\uff09<\/li>\n<\/ol>\n<h4>4.2 \u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u6700\u4f73\u5b9e\u8df5<\/h4>\n<p>\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u65f6\uff0c\u5e94\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li><strong>\u7ee7\u627f\u9002\u5f53\u7684\u5f02\u5e38\u7c7b<\/strong>\uff1a\u6839\u636e\u5f02\u5e38\u7684\u6027\u8d28\uff0c\u7ee7\u627f\u9002\u5f53\u7684\u5185\u7f6e\u5f02\u5e38\u7c7b<\/li>\n<li><strong>\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u5f02\u5e38\u4fe1\u606f<\/strong>\uff1a\u5728\u5f02\u5e38\u4fe1\u606f\u4e2d\u5305\u542b\u8db3\u591f\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f<\/li>\n<li><strong>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u5c5e\u6027<\/strong>\uff1a\u6839\u636e\u9700\u8981\u6dfb\u52a0\u81ea\u5b9a\u4e49\u5c5e\u6027\uff0c\u4ee5\u63d0\u4f9b\u66f4\u591a\u5173\u4e8e\u5f02\u5e38\u7684\u4fe1\u606f<\/li>\n<li><strong>\u4fdd\u6301\u5f02\u5e38\u7c7b\u7684\u7b80\u6d01<\/strong>\uff1a\u5f02\u5e38\u7c7b\u5e94\u8be5\u4fdd\u6301\u7b80\u6d01\uff0c\u53ea\u5305\u542b\u5fc5\u8981\u7684\u4ee3\u7801<\/li>\n<li><strong>\u4f7f\u7528\u5f02\u5e38\u5c42\u6b21\u7ed3\u6784<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u521b\u5efa\u5f02\u5e38\u5c42\u6b21\u7ed3\u6784<\/li>\n<\/ul>\n<h4>4.3 \u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u5e94\u7528\u573a\u666f<\/h4>\n<p>\u81ea\u5b9a\u4e49\u5f02\u5e38\u9002\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<ul>\n<li><strong>\u4e1a\u52a1\u903b\u8f91\u9519\u8bef<\/strong>\uff1a\u8868\u793a\u4e1a\u52a1\u903b\u8f91\u4e2d\u7684\u9519\u8bef\u60c5\u51b5<\/li>\n<li><strong>API\u9519\u8bef<\/strong>\uff1a\u8868\u793aAPI\u8c03\u7528\u4e2d\u7684\u9519\u8bef\u60c5\u51b5<\/li>\n<li><strong>\u914d\u7f6e\u9519\u8bef<\/strong>\uff1a\u8868\u793a\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u9519\u8bef\u60c5\u51b5<\/li>\n<li><strong>\u9a8c\u8bc1\u9519\u8bef<\/strong>\uff1a\u8868\u793a\u8f93\u5165\u9a8c\u8bc1\u4e2d\u7684\u9519\u8bef\u60c5\u51b5<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u81ea\u5b9a\u4e49\u5f02\u5e38\u793a\u4f8b\n\n# 1. \u57fa\u672c\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\nprint(&quot;\u57fa\u672c\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u793a\u4f8b\uff1a&quot;)\n\nclass ValidationError(Exception):\n    &quot;&quot;&quot;\u9a8c\u8bc1\u9519\u8bef\u5f02\u5e38&quot;&quot;&quot;\n    pass\n\ndef validate_email(email):\n    if '@' not in email:\n        raise ValidationError(f&quot;\u65e0\u6548\u7684\u90ae\u7bb1\u5730\u5740: {email}&quot;)\n    return email\n\ntry:\n    result = validate_email(&quot;invalid-email&quot;)\nexcept ValidationError as e:\n    print(f&quot;\u6355\u83b7\u5230\u9a8c\u8bc1\u9519\u8bef: {e}&quot;)\n\n# 2. \u5e26\u6709\u81ea\u5b9a\u4e49\u5c5e\u6027\u7684\u5f02\u5e38\u7c7b\nprint(&quot;\/n\u5e26\u6709\u81ea\u5b9a\u4e49\u5c5e\u6027\u7684\u5f02\u5e38\u7c7b\u793a\u4f8b\uff1a&quot;)\n\nclass APIError(Exception):\n    &quot;&quot;&quot;API\u9519\u8bef\u5f02\u5e38&quot;&quot;&quot;\n    def __init__(self, message, status_code, error_code):\n        super().__init__(message)\n        self.status_code = status_code\n        self.error_code = error_code\n    \n    def __str__(self):\n        return f&quot;APIError: {self.args[0]} (\u72b6\u6001\u7801: {self.status_code}, \u9519\u8bef\u7801: {self.error_code})&quot;\n\ndef call_api(endpoint):\n    if endpoint == &quot;\/error&quot;:\n        raise APIError(&quot;API\u8c03\u7528\u5931\u8d25&quot;, 500, &quot;INTERNAL_SERVER_ERROR&quot;)\n    return &quot;API\u8c03\u7528\u6210\u529f&quot;\n\ntry:\n    result = call_api(&quot;\/error&quot;)\nexcept APIError as e:\n    print(f&quot;\u6355\u83b7\u5230API\u9519\u8bef: {e}&quot;)\n    print(f&quot;\u72b6\u6001\u7801: {e.status_code}&quot;)\n    print(f&quot;\u9519\u8bef\u7801: {e.error_code}&quot;)\n\n# 3. \u5f02\u5e38\u5c42\u6b21\u7ed3\u6784\nprint(&quot;\/n\u5f02\u5e38\u5c42\u6b21\u7ed3\u6784\u793a\u4f8b\uff1a&quot;)\n\nclass AppError(Exception):\n    &quot;&quot;&quot;\u5e94\u7528\u7a0b\u5e8f\u57fa\u7840\u5f02\u5e38&quot;&quot;&quot;\n    pass\n\nclass DatabaseError(AppError):\n    &quot;&quot;&quot;\u6570\u636e\u5e93\u9519\u8bef&quot;&quot;&quot;\n    pass\n\nclass NetworkError(AppError):\n    &quot;&quot;&quot;\u7f51\u7edc\u9519\u8bef&quot;&quot;&quot;\n    pass\n\nclass ConnectionError(NetworkError):\n    &quot;&quot;&quot;\u8fde\u63a5\u9519\u8bef&quot;&quot;&quot;\n    pass\n\nclass TimeoutError(NetworkError):\n    &quot;&quot;&quot;\u8d85\u65f6\u9519\u8bef&quot;&quot;&quot;\n    pass\n\ndef connect_to_database():\n    raise DatabaseError(&quot;\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25&quot;)\n\ndef connect_to_api():\n    raise ConnectionError(&quot;API\u8fde\u63a5\u5931\u8d25&quot;)\n\ntry:\n    connect_to_database()\nexcept AppError as e:\n    print(f&quot;\u6355\u83b7\u5230\u5e94\u7528\u7a0b\u5e8f\u9519\u8bef: {e}&quot;)\n\ntry:\n    connect_to_api()\nexcept NetworkError as e:\n    print(f&quot;\u6355\u83b7\u5230\u7f51\u7edc\u9519\u8bef: {e}&quot;)\nexcept AppError as e:\n    print(f&quot;\u6355\u83b7\u5230\u5e94\u7528\u7a0b\u5e8f\u9519\u8bef: {e}&quot;)\n\n# 4. \u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u5b9e\u9645\u5e94\u7528\nprint(&quot;\/n\u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u5b9e\u9645\u5e94\u7528\u793a\u4f8b\uff1a&quot;)\n\nclass ConfigurationError(AppError):\n    &quot;&quot;&quot;\u914d\u7f6e\u9519\u8bef&quot;&quot;&quot;\n    def __init__(self, message, config_key=None):\n        super().__init__(message)\n        self.config_key = config_key\n\nclass InputError(AppError):\n    &quot;&quot;&quot;\u8f93\u5165\u9519\u8bef&quot;&quot;&quot;\n    def __init__(self, message, input_value=None):\n        super().__init__(message)\n        self.input_value = input_value\n\ndef load_config(config):\n    if &quot;database&quot; not in config:\n        raise ConfigurationError(&quot;\u914d\u7f6e\u4e2d\u7f3a\u5c11\u6570\u636e\u5e93\u4fe1\u606f&quot;, &quot;database&quot;)\n    if &quot;host&quot; not in config[&quot;database&quot;]:\n        raise ConfigurationError(&quot;\u914d\u7f6e\u4e2d\u7f3a\u5c11\u6570\u636e\u5e93\u4e3b\u673a\u4fe1\u606f&quot;, &quot;database.host&quot;)\n    return config\n\ndef process_input(value):\n    if not isinstance(value, int):\n        raise InputError(&quot;\u8f93\u5165\u503c\u5fc5\u987b\u662f\u6574\u6570&quot;, value)\n    if value &lt; 0:\n        raise InputError(&quot;\u8f93\u5165\u503c\u5fc5\u987b\u662f\u975e\u8d1f\u6570&quot;, value)\n    return value\n\ntry:\n    config = load_config({&quot;api&quot;: {&quot;key&quot;: &quot;secret&quot;}})\nexcept ConfigurationError as e:\n    print(f&quot;\u6355\u83b7\u5230\u914d\u7f6e\u9519\u8bef: {e}&quot;)\n    if e.config_key:\n        print(f&quot;\u914d\u7f6e\u952e: {e.config_key}&quot;)\n\ntry:\n    result = process_input(-5)\nexcept InputError as e:\n    print(f&quot;\u6355\u83b7\u5230\u8f93\u5165\u9519\u8bef: {e}&quot;)\n    if e.input_value is not None:\n        print(f&quot;\u8f93\u5165\u503c: {e.input_value}&quot;)<\/code><\/pre>\n<h3>5. \u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<p>\u5f02\u5e38\u5904\u7406\u662fPython\u7f16\u7a0b\u4e2d\u7684\u91cd\u8981\u90e8\u5206\uff0c\u6b63\u786e\u7684\u5f02\u5e38\u5904\u7406\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ee\u548c\u53ef\u9760\u3002\u4ee5\u4e0b\u662f\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<h4>5.1 \u5f02\u5e38\u5904\u7406\u7684\u539f\u5219<\/h4>\n<ul>\n<li><strong>\u53ea\u6355\u83b7\u5fc5\u8981\u7684\u5f02\u5e38<\/strong>\uff1a\u53ea\u6355\u83b7\u4f60\u80fd\u591f\u5904\u7406\u7684\u5f02\u5e38\uff0c\u4e0d\u8981\u6355\u83b7\u6240\u6709\u5f02\u5e38<\/li>\n<li><strong>\u4f7f\u7528\u5177\u4f53\u7684\u5f02\u5e38\u7c7b\u578b<\/strong>\uff1a\u5c3d\u91cf\u4f7f\u7528\u5177\u4f53\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u6355\u83b7\u6240\u6709\u5f02\u5e38<\/li>\n<li><strong>\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u5f02\u5e38\u4fe1\u606f<\/strong>\uff1a\u5728\u5f02\u5e38\u4fe1\u606f\u4e2d\u5305\u542b\u8db3\u591f\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f<\/li>\n<li><strong>\u4e0d\u8981\u5ffd\u7565\u5f02\u5e38<\/strong>\uff1a\u4e0d\u8981\u6355\u83b7\u5f02\u5e38\u540e\u4e0d\u505a\u4efb\u4f55\u5904\u7406<\/li>\n<li><strong>\u53ca\u65f6\u91ca\u653e\u8d44\u6e90<\/strong>\uff1a\u4f7f\u7528<code>finally<\/code>\u5b50\u53e5\u6216\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u786e\u4fdd\u8d44\u6e90\u7684\u91ca\u653e<\/li>\n<li><strong>\u4fdd\u6301\u5f02\u5e38\u5904\u7406\u7684\u7b80\u6d01<\/strong>\uff1a\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u5e94\u8be5\u4fdd\u6301\u7b80\u6d01\uff0c\u53ea\u5305\u542b\u5fc5\u8981\u7684\u4ee3\u7801<\/li>\n<li><strong>\u4f7f\u7528\u5f02\u5e38\u8fdb\u884c\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u4f7f\u7528\u5f02\u5e38\u6765\u5904\u7406\u9519\u8bef\u60c5\u51b5\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u8fd4\u56de\u503c<\/li>\n<\/ul>\n<h4>5.2 \u5f02\u5e38\u5904\u7406\u7684\u5e38\u89c1\u9519\u8bef<\/h4>\n<ul>\n<li><strong>\u8fc7\u5ea6\u4f7f\u7528\u5f02\u5e38<\/strong>\uff1a\u4e0d\u8981\u4f7f\u7528\u5f02\u5e38\u6765\u63a7\u5236\u6b63\u5e38\u7684\u7a0b\u5e8f\u6d41\u7a0b<\/li>\n<li><strong>\u6355\u83b7\u6240\u6709\u5f02\u5e38<\/strong>\uff1a\u4e0d\u8981\u6355\u83b7\u6240\u6709\u5f02\u5e38\uff0c\u8fd9\u4f1a\u63a9\u76d6\u771f\u6b63\u7684\u95ee\u9898<\/li>\n<li><strong>\u5ffd\u7565\u5f02\u5e38<\/strong>\uff1a\u4e0d\u8981\u6355\u83b7\u5f02\u5e38\u540e\u4e0d\u505a\u4efb\u4f55\u5904\u7406<\/li>\n<li><strong>\u5f02\u5e38\u4fe1\u606f\u4e0d\u660e\u786e<\/strong>\uff1a\u5f02\u5e38\u4fe1\u606f\u5e94\u8be5\u6e05\u6670\u660e\u786e\uff0c\u5305\u542b\u8db3\u591f\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f<\/li>\n<li><strong>\u8d44\u6e90\u6cc4\u9732<\/strong>\uff1a\u786e\u4fdd\u5728\u5f02\u5e38\u53d1\u751f\u65f6\u91ca\u653e\u8d44\u6e90<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406\u7684\u5d4c\u5957\u8fc7\u6df1<\/strong>\uff1a\u907f\u514d\u5f02\u5e38\u5904\u7406\u7684\u5d4c\u5957\u8fc7\u6df1\uff0c\u8fd9\u4f1a\u4f7f\u4ee3\u7801\u96be\u4ee5\u7406\u89e3<\/li>\n<\/ul>\n<h4>5.3 \u5f02\u5e38\u5904\u7406\u7684\u6a21\u5f0f<\/h4>\n<h5>5.3.1 EAFP\u6a21\u5f0f<\/h5>\n<p>EAFP\uff08Easier to Ask for Forgiveness than Permission\uff09\u662fPython\u4e2d\u7684\u4e00\u79cd\u7f16\u7a0b\u6a21\u5f0f\uff0c\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\uff1a\u5148\u5c1d\u8bd5\u6267\u884c\u64cd\u4f5c\uff0c\u5982\u679c\u53d1\u751f\u5f02\u5e38\u518d\u5904\u7406\u3002\u8fd9\u79cd\u6a21\u5f0f\u5728Python\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"python\">try:\n    value = dictionary[key]\nexcept KeyError:\n    value = default_value<\/code><\/pre>\n<h5>5.3.2 LBYL\u6a21\u5f0f<\/h5>\n<p>LBYL\uff08Look Before You Leap\uff09\u662f\u53e6\u4e00\u79cd\u7f16\u7a0b\u6a21\u5f0f\uff0c\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\uff1a\u5728\u6267\u884c\u64cd\u4f5c\u4e4b\u524d\u5148\u68c0\u67e5\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u6ee1\u8db3\u518d\u6267\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"python\">if key in dictionary:\n    value = dictionary[key]\nelse:\n    value = default_value<\/code><\/pre>\n<p>\u5728Python\u4e2d\uff0cEAFP\u6a21\u5f0f\u901a\u5e38\u6bd4LBYL\u6a21\u5f0f\u66f4\u53d7\u6b22\u8fce\uff0c\u56e0\u4e3a\u5b83\u66f4\u7b80\u6d01\uff0c\u5e76\u4e14\u5728\u5e76\u53d1\u73af\u5883\u4e2d\u66f4\u5b89\u5168\u3002<\/p>\n<h4>5.4 \u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u8003\u8651<\/h4>\n<p>\u5f02\u5e38\u5904\u7406\u4f1a\u5bf9\u7a0b\u5e8f\u7684\u6027\u80fd\u4ea7\u751f\u4e00\u5b9a\u7684\u5f71\u54cd\uff0c\u56e0\u6b64\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u5e94\u8be5\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li><strong>\u5f02\u5e38\u53ea\u7528\u4e8e\u5f02\u5e38\u60c5\u51b5<\/strong>\uff1a\u4e0d\u8981\u4f7f\u7528\u5f02\u5e38\u6765\u63a7\u5236\u6b63\u5e38\u7684\u7a0b\u5e8f\u6d41\u7a0b<\/li>\n<li><strong>\u907f\u514d\u5728\u5faa\u73af\u4e2d\u5f15\u53d1\u5f02\u5e38<\/strong>\uff1a\u5728\u5faa\u73af\u4e2d\u5f15\u53d1\u5f02\u5e38\u4f1a\u663e\u8457\u964d\u4f4e\u7a0b\u5e8f\u7684\u6027\u80fd<\/li>\n<li><strong>\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf<\/strong>\uff1a\u5728\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u4e2d\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u5168\u5c40\u53d8\u91cf<\/li>\n<li><strong>\u4fdd\u6301\u5f02\u5e38\u5904\u7406\u7684\u7b80\u6d01<\/strong>\uff1a\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u5e94\u8be5\u4fdd\u6301\u7b80\u6d01\uff0c\u53ea\u5305\u542b\u5fc5\u8981\u7684\u4ee3\u7801<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u793a\u4f8b\n\n# 1. \u53ea\u6355\u83b7\u5fc5\u8981\u7684\u5f02\u5e38\nprint(&quot;\u53ea\u6355\u83b7\u5fc5\u8981\u7684\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\ntry:\n    num = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570: &quot;))\n    result = 10 \/ num\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u9519\u8bef\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570&quot;)\nexcept ZeroDivisionError:\n    print(&quot;\u9519\u8bef\uff1a\u4e0d\u80fd\u9664\u4ee5\u96f6&quot;)\n# \u4e0d\u8981\u8fd9\u6837\u505a\n# except Exception:\n#     print(&quot;\u53d1\u751f\u9519\u8bef&quot;)\n\n# 2. \u63d0\u4f9b\u6709\u610f\u4e49\u7684\u5f02\u5e38\u4fe1\u606f\nprint(&quot;\/n\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u5f02\u5e38\u4fe1\u606f\u793a\u4f8b\uff1a&quot;)\n\ndef divide(a, b):\n    if b == 0:\n        raise ZeroDivisionError(f&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6 (a={a}, b={b})&quot;)\n    return a \/ b\n\ntry:\n    result = divide(10, 0)\nexcept ZeroDivisionError as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n\n# 3. \u4f7f\u7528finally\u5b50\u53e5\u91ca\u653e\u8d44\u6e90\nprint(&quot;\/n\u4f7f\u7528finally\u5b50\u53e5\u91ca\u653e\u8d44\u6e90\u793a\u4f8b\uff1a&quot;)\n\ndef read_file(filename):\n    f = None\n    try:\n        f = open(filename, &quot;r&quot;)\n        content = f.read()\n        return content\n    except FileNotFoundError:\n        print(f&quot;\u6587\u4ef6\u4e0d\u5b58\u5728: {filename}&quot;)\n        return &quot;&quot;\n    finally:\n        if f is not None:\n            f.close()\n            print(&quot;\u6587\u4ef6\u5df2\u5173\u95ed&quot;)\n\ncontent = read_file(&quot;non_existent_file.txt&quot;)\nprint(f&quot;\u6587\u4ef6\u5185\u5bb9: {content}&quot;)\n\n# 4. \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\nprint(&quot;\/n\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u793a\u4f8b\uff1a&quot;)\n\ndef read_file_safely(filename):\n    try:\n        with open(filename, &quot;r&quot;) as f:\n            content = f.read()\n        return content\n    except FileNotFoundError:\n        print(f&quot;\u6587\u4ef6\u4e0d\u5b58\u5728: {filename}&quot;)\n        return &quot;&quot;\n\ncontent = read_file_safely(&quot;non_existent_file.txt&quot;)\nprint(f&quot;\u6587\u4ef6\u5185\u5bb9: {content}&quot;)\n\n# 5. EAFP vs LBYL\u6a21\u5f0f\nprint(&quot;\/nEAFP vs LBYL\u6a21\u5f0f\u793a\u4f8b\uff1a&quot;)\n\n# EAFP\u6a21\u5f0f\ndictionary = {&quot;a&quot;: 1, &quot;b&quot;: 2}\nkey = &quot;c&quot;\n\ntry:\n    value = dictionary[key]\n    print(f&quot;EAFP\u6a21\u5f0f: \u627e\u5230\u503c: {value}&quot;)\nexcept KeyError:\n    value = &quot;\u9ed8\u8ba4\u503c&quot;\n    print(f&quot;EAFP\u6a21\u5f0f: \u952e\u4e0d\u5b58\u5728\uff0c\u4f7f\u7528\u9ed8\u8ba4\u503c: {value}&quot;)\n\n# LBYL\u6a21\u5f0f\nif key in dictionary:\n    value = dictionary[key]\n    print(f&quot;LBYL\u6a21\u5f0f: \u627e\u5230\u503c: {value}&quot;)\nelse:\n    value = &quot;\u9ed8\u8ba4\u503c&quot;\n    print(f&quot;LBYL\u6a21\u5f0f: \u952e\u4e0d\u5b58\u5728\uff0c\u4f7f\u7528\u9ed8\u8ba4\u503c: {value}&quot;)\n\n# 6. \u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u8003\u8651\nprint(&quot;\/n\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u8003\u8651\u793a\u4f8b\uff1a&quot;)\n\nimport time\n\n# \u6d4b\u8bd5\u6b63\u5e38\u60c5\u51b5\u4e0b\u7684\u6027\u80fd\ndef test_normal_case():\n    start = time.time()\n    for i in range(1000000):\n        # \u6b63\u5e38\u6d41\u7a0b\n        pass\n    end = time.time()\n    print(f&quot;\u6b63\u5e38\u60c5\u51b5\u8017\u65f6: {end - start:.4f}\u79d2&quot;)\n\n# \u6d4b\u8bd5\u5f02\u5e38\u60c5\u51b5\u4e0b\u7684\u6027\u80fd\ndef test_exception_case():\n    start = time.time()\n    for i in range(1000000):\n        try:\n            # \u5c1d\u8bd5\u6267\u884c\u64cd\u4f5c\n            pass\n        except Exception:\n            # \u6355\u83b7\u5f02\u5e38\n            pass\n    end = time.time()\n    print(f&quot;\u5f02\u5e38\u5904\u7406\u8017\u65f6: {end - start:.4f}\u79d2&quot;)\n\n# \u6d4b\u8bd5\u5f15\u53d1\u5f02\u5e38\u7684\u6027\u80fd\ndef test_raise_exception():\n    start = time.time()\n    for i in range(1000):\n        try:\n            raise Exception(&quot;\u6d4b\u8bd5\u5f02\u5e38&quot;)\n        except Exception:\n            pass\n    end = time.time()\n    print(f&quot;\u5f15\u53d1\u5f02\u5e38\u8017\u65f6: {end - start:.4f}\u79d2&quot;)\n\ntest_normal_case()\ntest_exception_case()\ntest_raise_exception()\n\n# 7. \u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u6700\u4f73\u5b9e\u8df5\nprint(&quot;\/n\u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u6700\u4f73\u5b9e\u8df5\u793a\u4f8b\uff1a&quot;)\n\nclass BusinessLogicError(Exception):\n    &quot;&quot;&quot;\u4e1a\u52a1\u903b\u8f91\u9519\u8bef&quot;&quot;&quot;\n    def __init__(self, message, error_code):\n        super().__init__(message)\n        self.error_code = error_code\n    \n    def to_dict(self):\n        &quot;&quot;&quot;\u5c06\u5f02\u5e38\u8f6c\u6362\u4e3a\u5b57\u5178&quot;&quot;&quot;\n        return {\n            &quot;error&quot;: self.__class__.__name__,\n            &quot;message&quot;: self.args[0],\n            &quot;error_code&quot;: self.error_code\n        }\n\ndef process_order(order):\n    if not order.get(&quot;customer_id&quot;):\n        raise BusinessLogicError(&quot;\u8ba2\u5355\u7f3a\u5c11\u5ba2\u6237ID&quot;, &quot;MISSING_CUSTOMER_ID&quot;)\n    if order.get(&quot;amount&quot;) &lt;= 0:\n        raise BusinessLogicError(&quot;\u8ba2\u5355\u91d1\u989d\u5fc5\u987b\u5927\u4e8e\u96f6&quot;, &quot;INVALID_AMOUNT&quot;)\n    return &quot;\u8ba2\u5355\u5904\u7406\u6210\u529f&quot;\n\ntry:\n    order = {&quot;amount&quot;: -100}\n    result = process_order(order)\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nexcept BusinessLogicError as e:\n    error_info = e.to_dict()\n    print(f&quot;\u6355\u83b7\u5230\u4e1a\u52a1\u903b\u8f91\u9519\u8bef: {error_info}&quot;)<\/code><\/pre>\n<h3>6. \u5f02\u5e38\u5904\u7406\u4e0e\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<p>\u5f02\u5e38\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u662fPython\u7f16\u7a0b\u4e2d\u7684\u4e24\u4e2a\u91cd\u8981\u90e8\u5206\uff0c\u5b83\u4eec\u901a\u5e38\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<h4>6.1 \u65e5\u5fd7\u8bb0\u5f55\u7684\u57fa\u672c\u6982\u5ff5<\/h4>\n<p>\u65e5\u5fd7\u8bb0\u5f55\u662f\u6307\u5c06\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u4fe1\u606f\u8bb0\u5f55\u5230\u6587\u4ef6\u6216\u5176\u4ed6\u8f93\u51fa\u8bbe\u5907\u4e2d\u3002Python\u7684<code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7075\u6d3b\u7684\u65e5\u5fd7\u8bb0\u5f55\u7cfb\u7edf\u3002<\/p>\n<h4>6.2 \u5f02\u5e38\u5904\u7406\u4e0e\u65e5\u5fd7\u8bb0\u5f55\u7684\u7ed3\u5408<\/h4>\n<p>\u5728\u5f02\u5e38\u5904\u7406\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f\uff0c\u4ee5\u4fbf\u4e8e\u8c03\u8bd5\u548c\u95ee\u9898\u6392\u67e5\u3002\u4ee5\u4e0b\u662f\u5f02\u5e38\u5904\u7406\u4e0e\u65e5\u5fd7\u8bb0\u5f55\u7ed3\u5408\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li><strong>\u8bb0\u5f55\u5f02\u5e38\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong>\uff1a\u4f7f\u7528<code>logging.exception()<\/code>\u51fd\u6570\u8bb0\u5f55\u5f02\u5e38\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u5806\u6808\u8ddf\u8e2a<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u9002\u5f53\u7684\u65e5\u5fd7\u7ea7\u522b<\/strong>\uff1a\u6839\u636e\u5f02\u5e38\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u4f7f\u7528\u9002\u5f53\u7684\u65e5\u5fd7\u7ea7\u522b<\/p>\n<ul>\n<li><code>DEBUG<\/code>\uff1a\u8be6\u7ec6\u7684\u8c03\u8bd5\u4fe1\u606f<\/li>\n<li><code>INFO<\/code>\uff1a\u4e00\u822c\u4fe1\u606f<\/li>\n<li><code>WARNING<\/code>\uff1a\u8b66\u544a\u4fe1\u606f<\/li>\n<li><code>ERROR<\/code>\uff1a\u9519\u8bef\u4fe1\u606f<\/li>\n<li><code>CRITICAL<\/code>\uff1a\u4e25\u91cd\u9519\u8bef\u4fe1\u606f<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5305\u542b\u4e0a\u4e0b\u6587\u4fe1\u606f<\/strong>\uff1a\u5728\u65e5\u5fd7\u4e2d\u5305\u542b\u8db3\u591f\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u4ee5\u4fbf\u4e8e\u7406\u89e3\u5f02\u5e38\u7684\u539f\u56e0<\/li>\n<li><strong>\u533a\u5206\u7528\u6237\u9519\u8bef\u548c\u7cfb\u7edf\u9519\u8bef<\/strong>\uff1a\u533a\u5206\u7528\u6237\u9519\u8bef\u548c\u7cfb\u7edf\u9519\u8bef\uff0c\u4f7f\u7528\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f<\/li>\n<\/ul>\n<h4>6.3 \u65e5\u5fd7\u8bb0\u5f55\u7684\u914d\u7f6e<\/h4>\n<p>Python\u7684<code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u914d\u7f6e\u9009\u9879\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7684\u884c\u4e3a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u914d\u7f6e\u9009\u9879\uff1a<\/p>\n<ul>\n<li><strong>\u65e5\u5fd7\u7ea7\u522b<\/strong>\uff1a\u8bbe\u7f6e\u65e5\u5fd7\u7684\u6700\u4f4e\u7ea7\u522b<\/li>\n<li><strong>\u8f93\u51fa\u683c\u5f0f<\/strong>\uff1a\u8bbe\u7f6e\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f<\/li>\n<li><strong>\u8f93\u51fa\u76ee\u6807<\/strong>\uff1a\u8bbe\u7f6e\u65e5\u5fd7\u7684\u8f93\u51fa\u76ee\u6807\uff08\u63a7\u5236\u53f0\u3001\u6587\u4ef6\u7b49\uff09<\/li>\n<li><strong>\u65e5\u5fd7\u8f6e\u8f6c<\/strong>\uff1a\u8bbe\u7f6e\u65e5\u5fd7\u6587\u4ef6\u7684\u8f6e\u8f6c\u7b56\u7565<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u5f02\u5e38\u5904\u7406\u4e0e\u65e5\u5fd7\u8bb0\u5f55\u793a\u4f8b\n\nimport logging\n\n# \u914d\u7f6e\u65e5\u5fd7\nlogging.basicConfig(\n    level=logging.DEBUG,\n    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',\n    filename='app.log',\n    filemode='a'\n)\n\n# \u521b\u5efalogger\nlogger = logging.getLogger(__name__)\n\n# 1. \u57fa\u672c\u7684\u5f02\u5e38\u65e5\u5fd7\u8bb0\u5f55\nprint(&quot;\u57fa\u672c\u7684\u5f02\u5e38\u65e5\u5fd7\u8bb0\u5f55\u793a\u4f8b\uff1a&quot;)\n\ndef divide(a, b):\n    try:\n        return a \/ b\n    except ZeroDivisionError as e:\n        logger.error(f&quot;\u9664\u4ee5\u96f6\u9519\u8bef: a={a}, b={b}&quot;, exc_info=True)\n        raise\n\ntry:\n    result = divide(10, 0)\nexcept ZeroDivisionError as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n\n# 2. \u4f7f\u7528logging.exception()\nprint(&quot;\/n\u4f7f\u7528logging.exception()\u793a\u4f8b\uff1a&quot;)\n\ndef read_config(filename):\n    try:\n        with open(filename, &quot;r&quot;) as f:\n            content = f.read()\n        return content\n    except Exception as e:\n        logger.exception(f&quot;\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u5931\u8d25: {filename}&quot;)\n        raise\n\ntry:\n    content = read_config(&quot;non_existent_config.json&quot;)\nexcept Exception as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n\n# 3. \u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\nprint(&quot;\/n\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u793a\u4f8b\uff1a&quot;)\n\ndef process_data(data):\n    if not data:\n        logger.warning(&quot;\u6570\u636e\u4e3a\u7a7a&quot;)\n        return []\n    try:\n        processed_data = [int(item) for item in data.split(',')]\n        logger.info(f&quot;\u6210\u529f\u5904\u7406\u6570\u636e: {data}&quot;)\n        return processed_data\n    except ValueError as e:\n        logger.error(f&quot;\u6570\u636e\u5904\u7406\u5931\u8d25: {data}&quot;, exc_info=True)\n        raise\n\ntry:\n    result = process_data(&quot;&quot;)\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nexcept Exception as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n\ntry:\n    result = process_data(&quot;1,2,3&quot;)\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nexcept Exception as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n\ntry:\n    result = process_data(&quot;1,2,abc&quot;)\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nexcept Exception as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n\n# 4. \u65e5\u5fd7\u914d\u7f6e\u793a\u4f8b\nprint(&quot;\/n\u65e5\u5fd7\u914d\u7f6e\u793a\u4f8b\uff1a&quot;)\n\n# \u66f4\u590d\u6742\u7684\u65e5\u5fd7\u914d\u7f6e\nimport logging.config\n\nlogging_config = {\n    'version': 1,\n    'disable_existing_loggers': False,\n    'formatters': {\n        'standard': {\n            'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'\n        },\n        'detailed': {\n            'format': '%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(lineno)d - %(message)s'\n        }\n    },\n    'handlers': {\n        'console': {\n            'class': 'logging.StreamHandler',\n            'level': 'INFO',\n            'formatter': 'standard',\n            'stream': 'ext:\/\/sys.stdout'\n        },\n        'file': {\n            'class': 'logging.handlers.RotatingFileHandler',\n            'level': 'DEBUG',\n            'formatter': 'detailed',\n            'filename': 'app.log',\n            'maxBytes': 10485760,  # 10MB\n            'backupCount': 5\n        }\n    },\n    'loggers': {\n        '': {\n            'handlers': ['console', 'file'],\n            'level': 'DEBUG',\n            'propagate': True\n        }\n    }\n}\n\nlogging.config.dictConfig(logging_config)\n\n# \u6d4b\u8bd5\u914d\u7f6e\u540e\u7684\u65e5\u5fd7\nlogger = logging.getLogger(__name__)\nlogger.debug(&quot;\u8fd9\u662f\u4e00\u6761\u8c03\u8bd5\u4fe1\u606f&quot;)\nlogger.info(&quot;\u8fd9\u662f\u4e00\u6761\u4e00\u822c\u4fe1\u606f&quot;)\nlogger.warning(&quot;\u8fd9\u662f\u4e00\u6761\u8b66\u544a\u4fe1\u606f&quot;)\nlogger.error(&quot;\u8fd9\u662f\u4e00\u6761\u9519\u8bef\u4fe1\u606f&quot;)\nlogger.critical(&quot;\u8fd9\u662f\u4e00\u6761\u4e25\u91cd\u9519\u8bef\u4fe1\u606f&quot;)\n\n# 5. \u5f02\u5e38\u5904\u7406\u4e0e\u65e5\u5fd7\u8bb0\u5f55\u7684\u6700\u4f73\u5b9e\u8df5\nprint(&quot;\/n\u5f02\u5e38\u5904\u7406\u4e0e\u65e5\u5fd7\u8bb0\u5f55\u7684\u6700\u4f73\u5b9e\u8df5\u793a\u4f8b\uff1a&quot;)\n\ndef safe_operation(func):\n    &quot;&quot;&quot;\u5b89\u5168\u64cd\u4f5c\u88c5\u9970\u5668&quot;&quot;&quot;\n    def wrapper(*args, **kwargs):\n        try:\n            return func(*args, **kwargs)\n        except Exception as e:\n            logger.exception(f&quot;\u64cd\u4f5c\u5931\u8d25: {func.__name__}&quot;)\n            raise\n    return wrapper\n\n@safe_operation\ndef risky_operation():\n    &quot;&quot;&quot; risky operation &quot;&quot;&quot;\n    10 \/ 0\n\ntry:\n    risky_operation()\nexcept Exception as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n\n# 6. \u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u65e5\u5fd7\u8bb0\u5f55\nprint(&quot;\/n\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u65e5\u5fd7\u8bb0\u5f55\u793a\u4f8b\uff1a&quot;)\n\nclass AppException(Exception):\n    &quot;&quot;&quot;\u5e94\u7528\u7a0b\u5e8f\u5f02\u5e38&quot;&quot;&quot;\n    def __init__(self, message, error_code, level=logging.ERROR):\n        super().__init__(message)\n        self.error_code = error_code\n        self.level = level\n    \n    def log(self, logger):\n        &quot;&quot;&quot;\u8bb0\u5f55\u5f02\u5e38&quot;&quot;&quot;\n        if self.level == logging.ERROR:\n            logger.exception(f&quot;{self.__class__.__name__}: {self.args[0]} (\u9519\u8bef\u7801: {self.error_code})&quot;)\n        else:\n            logger.log(self.level, f&quot;{self.__class__.__name__}: {self.args[0]} (\u9519\u8bef\u7801: {self.error_code})&quot;)\n\nclass ValidationError(AppException):\n    &quot;&quot;&quot;\u9a8c\u8bc1\u9519\u8bef&quot;&quot;&quot;\n    def __init__(self, message, field):\n        super().__init__(message, &quot;VALIDATION_ERROR&quot;, logging.WARNING)\n        self.field = field\n    \n    def log(self, logger):\n        logger.warning(f&quot;ValidationError: {self.args[0]} (\u5b57\u6bb5: {self.field})&quot;)\n\ndef validate_user(user):\n    if not user.get(&quot;name&quot;):\n        raise ValidationError(&quot;\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a&quot;, &quot;name&quot;)\n    if not user.get(&quot;email&quot;):\n        raise ValidationError(&quot;\u90ae\u7bb1\u4e0d\u80fd\u4e3a\u7a7a&quot;, &quot;email&quot;)\n    if &quot;@&quot; not in user.get(&quot;email&quot;, &quot;&quot;):\n        raise ValidationError(&quot;\u90ae\u7bb1\u683c\u5f0f\u65e0\u6548&quot;, &quot;email&quot;)\n    return True\n\ntry:\n    user = {&quot;name&quot;: &quot;John&quot;}\n    validate_user(user)\n    print(&quot;\u7528\u6237\u9a8c\u8bc1\u6210\u529f&quot;)\nexcept AppException as e:\n    e.log(logger)\n    print(f&quot;\u6355\u83b7\u5230\u5e94\u7528\u7a0b\u5e8f\u5f02\u5e38: {e}&quot;)<\/code><\/pre>\n<h3>7. \u5f02\u5e38\u5904\u7406\u4e0e\u6d4b\u8bd5<\/h3>\n<p>\u5f02\u5e38\u5904\u7406\u662fPython\u6d4b\u8bd5\u4e2d\u7684\u91cd\u8981\u90e8\u5206\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u5730\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<h4>7.1 \u6d4b\u8bd5\u5f02\u5e38\u7684\u57fa\u672c\u65b9\u6cd5<\/h4>\n<p>\u5728Python\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528<code>unittest<\/code>\u6a21\u5757\u6216<code>pytest<\/code>\u6846\u67b6\u6765\u6d4b\u8bd5\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u6d4b\u8bd5\u5f02\u5e38\u7684\u57fa\u672c\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528<code>assertRaises<\/code><\/strong>\uff1a\u6d4b\u8bd5\u4ee3\u7801\u662f\u5426\u4f1a\u5f15\u53d1\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38<\/li>\n<li><strong>\u4f7f\u7528<code>assertRaisesRegex<\/code><\/strong>\uff1a\u6d4b\u8bd5\u4ee3\u7801\u662f\u5426\u4f1a\u5f15\u53d1\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u5e76\u4e14\u5f02\u5e38\u4fe1\u606f\u5339\u914d\u7279\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/li>\n<li><strong>\u4f7f\u7528<code>pytest.raises<\/code><\/strong>\uff1a\u5728pytest\u4e2d\u6d4b\u8bd5\u4ee3\u7801\u662f\u5426\u4f1a\u5f15\u53d1\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38<\/li>\n<\/ul>\n<h4>7.2 \u6d4b\u8bd5\u5f02\u5e38\u7684\u6700\u4f73\u5b9e\u8df5<\/h4>\n<p>\u6d4b\u8bd5\u5f02\u5e38\u65f6\uff0c\u5e94\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li><strong>\u6d4b\u8bd5\u6240\u6709\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5<\/strong>\uff1a\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u6240\u6709\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u60c5\u51b5<\/li>\n<li><strong>\u6d4b\u8bd5\u5f02\u5e38\u7684\u7c7b\u578b<\/strong>\uff1a\u786e\u4fdd\u4ee3\u7801\u5f15\u53d1\u7684\u662f\u6b63\u786e\u7c7b\u578b\u7684\u5f02\u5e38<\/li>\n<li><strong>\u6d4b\u8bd5\u5f02\u5e38\u7684\u4fe1\u606f<\/strong>\uff1a\u786e\u4fdd\u5f02\u5e38\u4fe1\u606f\u6e05\u6670\u660e\u786e<\/li>\n<li><strong>\u6d4b\u8bd5\u5f02\u5e38\u7684\u5904\u7406<\/strong>\uff1a\u786e\u4fdd\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u5730\u5904\u7406\u5f02\u5e38<\/li>\n<li><strong>\u6d4b\u8bd5\u8fb9\u754c\u60c5\u51b5<\/strong>\uff1a\u6d4b\u8bd5\u8fb9\u754c\u60c5\u51b5\uff0c\u786e\u4fdd\u4ee3\u7801\u80fd\u591f\u6b63\u786e\u5730\u5904\u7406\u8fb9\u754c\u60c5\u51b5<\/li>\n<\/ul>\n<h4>7.3 \u5f02\u5e38\u5904\u7406\u7684\u6d4b\u8bd5\u793a\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528<code>unittest<\/code>\u6a21\u5757\u548c<code>pytest<\/code>\u6846\u67b6\u6d4b\u8bd5\u5f02\u5e38\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"python\"># \u5f02\u5e38\u5904\u7406\u4e0e\u6d4b\u8bd5\u793a\u4f8b\n\n# 1. \u4f7f\u7528unittest\u6a21\u5757\u6d4b\u8bd5\u5f02\u5e38\nprint(&quot;\u4f7f\u7528unittest\u6a21\u5757\u6d4b\u8bd5\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\nimport unittest\n\nclass Calculator:\n    def divide(self, a, b):\n        if b == 0:\n            raise ZeroDivisionError(&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;)\n        return a \/ b\n\nclass TestCalculator(unittest.TestCase):\n    def setUp(self):\n        self.calculator = Calculator()\n    \n    def test_divide_normal(self):\n        &quot;&quot;&quot;\u6d4b\u8bd5\u6b63\u5e38\u9664\u6cd5&quot;&quot;&quot;\n        result = self.calculator.divide(10, 2)\n        self.assertEqual(result, 5)\n    \n    def test_divide_zero(self):\n        &quot;&quot;&quot;\u6d4b\u8bd5\u9664\u4ee5\u96f6&quot;&quot;&quot;\n        with self.assertRaises(ZeroDivisionError) as cm:\n            self.calculator.divide(10, 0)\n        self.assertIn(&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;, str(cm.exception))\n\n# \u8fd0\u884c\u6d4b\u8bd5\nif __name__ == '__main__':\n    unittest.main(argv=['first-arg-is-ignored'], exit=False)\n\n# 2. \u4f7f\u7528pytest\u6d4b\u8bd5\u5f02\u5e38\nprint(&quot;\/n\u4f7f\u7528pytest\u6d4b\u8bd5\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\n# \u4ee5\u4e0b\u4ee3\u7801\u9700\u8981\u5728pytest\u73af\u5883\u4e2d\u8fd0\u884c\n'''\ndef test_divide_normal():\n    calculator = Calculator()\n    result = calculator.divide(10, 2)\n    assert result == 5\n\ndef test_divide_zero():\n    calculator = Calculator()\n    with pytest.raises(ZeroDivisionError, match=&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;):\n        calculator.divide(10, 0)\n'''\n\n# 3. \u6d4b\u8bd5\u81ea\u5b9a\u4e49\u5f02\u5e38\nprint(&quot;\/n\u6d4b\u8bd5\u81ea\u5b9a\u4e49\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\nclass ValidationError(Exception):\n    &quot;&quot;&quot;\u9a8c\u8bc1\u9519\u8bef&quot;&quot;&quot;\n    pass\n\ndef validate_email(email):\n    if '@' not in email:\n        raise ValidationError(f&quot;\u65e0\u6548\u7684\u90ae\u7bb1\u5730\u5740: {email}&quot;)\n    return email\n\nclass TestValidation(unittest.TestCase):\n    def test_validate_email_valid(self):\n        &quot;&quot;&quot;\u6d4b\u8bd5\u6709\u6548\u7684\u90ae\u7bb1\u5730\u5740&quot;&quot;&quot;\n        result = validate_email(&quot;test@example.com&quot;)\n        self.assertEqual(result, &quot;test@example.com&quot;)\n    \n    def test_validate_email_invalid(self):\n        &quot;&quot;&quot;\u6d4b\u8bd5\u65e0\u6548\u7684\u90ae\u7bb1\u5730\u5740&quot;&quot;&quot;\n        with self.assertRaises(ValidationError) as cm:\n            validate_email(&quot;invalid-email&quot;)\n        self.assertIn(&quot;\u65e0\u6548\u7684\u90ae\u7bb1\u5730\u5740&quot;, str(cm.exception))\n\n# \u8fd0\u884c\u6d4b\u8bd5\nif __name__ == '__main__':\n    unittest.main(argv=['first-arg-is-ignored'], exit=False)\n\n# 4. \u6d4b\u8bd5\u5f02\u5e38\u5904\u7406\u4ee3\u7801\nprint(&quot;\/n\u6d4b\u8bd5\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u793a\u4f8b\uff1a&quot;)\n\ndef safe_divide(a, b):\n    try:\n        return a \/ b\n    except ZeroDivisionError:\n        return float('inf')\n    except TypeError:\n        return None\n\nclass TestSafeDivide(unittest.TestCase):\n    def test_safe_divide_normal(self):\n        &quot;&quot;&quot;\u6d4b\u8bd5\u6b63\u5e38\u9664\u6cd5&quot;&quot;&quot;\n        result = safe_divide(10, 2)\n        self.assertEqual(result, 5)\n    \n    def test_safe_divide_zero(self):\n        &quot;&quot;&quot;\u6d4b\u8bd5\u9664\u4ee5\u96f6&quot;&quot;&quot;\n        result = safe_divide(10, 0)\n        self.assertEqual(result, float('inf'))\n    \n    def test_safe_divide_type_error(self):\n        &quot;&quot;&quot;\u6d4b\u8bd5\u7c7b\u578b\u9519\u8bef&quot;&quot;&quot;\n        result = safe_divide(10, &quot;2&quot;)\n        self.assertIsNone(result)\n\n# \u8fd0\u884c\u6d4b\u8bd5\nif __name__ == '__main__':\n    unittest.main(argv=['first-arg-is-ignored'], exit=False)\n\n# 5. \u6d4b\u8bd5\u5f02\u5e38\u7684\u8fb9\u754c\u60c5\u51b5\nprint(&quot;\/n\u6d4b\u8bd5\u5f02\u5e38\u7684\u8fb9\u754c\u60c5\u51b5\u793a\u4f8b\uff1a&quot;)\n\ndef process_list(items):\n    if not isinstance(items, list):\n        raise TypeError(&quot;\u53c2\u6570\u5fc5\u987b\u662f\u5217\u8868&quot;)\n    if not items:\n        raise ValueError(&quot;\u5217\u8868\u4e0d\u80fd\u4e3a\u7a7a&quot;)\n    return sum(items)\n\nclass TestProcessList(unittest.TestCase):\n    def test_process_list_normal(self):\n        &quot;&quot;&quot;\u6d4b\u8bd5\u6b63\u5e38\u60c5\u51b5&quot;&quot;&quot;\n        result = process_list([1, 2, 3])\n        self.assertEqual(result, 6)\n    \n    def test_process_list_not_list(self):\n        &quot;&quot;&quot;\u6d4b\u8bd5\u53c2\u6570\u4e0d\u662f\u5217\u8868&quot;&quot;&quot;\n        with self.assertRaises(TypeError) as cm:\n            process_list(&quot;not a list&quot;)\n        self.assertIn(&quot;\u53c2\u6570\u5fc5\u987b\u662f\u5217\u8868&quot;, str(cm.exception))\n    \n    def test_process_list_empty(self):\n        &quot;&quot;&quot;\u6d4b\u8bd5\u7a7a\u5217\u8868&quot;&quot;&quot;\n        with self.assertRaises(ValueError) as cm:\n            process_list([])\n        self.assertIn(&quot;\u5217\u8868\u4e0d\u80fd\u4e3a\u7a7a&quot;, str(cm.exception))\n\n# \u8fd0\u884c\u6d4b\u8bd5\nif __name__ == '__main__':\n    unittest.main(argv=['first-arg-is-ignored'], exit=False)\n\n# 6. \u4f7f\u7528mock\u6d4b\u8bd5\u5f02\u5e38\nprint(&quot;\/n\u4f7f\u7528mock\u6d4b\u8bd5\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\nfrom unittest.mock import Mock, patch\n\ndef get_data_from_api(url):\n    import requests\n    try:\n        response = requests.get(url)\n        response.raise_for_status()\n        return response.json()\n    except requests.exceptions.RequestException as e:\n        raise RuntimeError(f&quot;API\u8c03\u7528\u5931\u8d25: {e}&quot;)\n\nclass TestGetDataFromApi(unittest.TestCase):\n    @patch('requests.get')\n    def test_get_data_success(self, mock_get):\n        &quot;&quot;&quot;\u6d4b\u8bd5API\u8c03\u7528\u6210\u529f&quot;&quot;&quot;\n        mock_response = Mock()\n        mock_response.json.return_value = {&quot;data&quot;: &quot;test&quot;}\n        mock_response.raise_for_status.return_value = None\n        mock_get.return_value = mock_response\n        \n        result = get_data_from_api(&quot;https:\/\/example.com\/api&quot;)\n        self.assertEqual(result, {&quot;data&quot;: &quot;test&quot;})\n    \n    @patch('requests.get')\n    def test_get_data_failure(self, mock_get):\n        &quot;&quot;&quot;\u6d4b\u8bd5API\u8c03\u7528\u5931\u8d25&quot;&quot;&quot;\n        mock_response = Mock()\n        mock_response.raise_for_status.side_effect = Exception(&quot;API\u9519\u8bef&quot;)\n        mock_get.return_value = mock_response\n        \n        with self.assertRaises(RuntimeError) as cm:\n            get_data_from_api(&quot;https:\/\/example.com\/api&quot;)\n        self.assertIn(&quot;API\u8c03\u7528\u5931\u8d25&quot;, str(cm.exception))\n\n# \u8fd0\u884c\u6d4b\u8bd5\nif __name__ == '__main__':\n    unittest.main(argv=['first-arg-is-ignored'], exit=False)<\/code><\/pre>\n<h3>7. \u5f02\u5e38\u5904\u7406\u7684\u9ad8\u7ea7\u6280\u5de7<\/h3>\n<h4>7.1 \u4f7f\u7528\u88c5\u9970\u5668\u5904\u7406\u5f02\u5e38<\/h4>\n<p>\u88c5\u9970\u5668\u662fPython\u4e2d\u7684\u4e00\u79cd\u9ad8\u7ea7\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6765\u7edf\u4e00\u5904\u7406\u51fd\u6570\u6216\u65b9\u6cd5\u4e2d\u7684\u5f02\u5e38\u3002<\/p>\n<h4>7.2 \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5904\u7406\u5f02\u5e38<\/h4>\n<p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662fPython\u4e2d\u7684\u4e00\u79cd\u9ad8\u7ea7\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u7ba1\u7406\u8d44\u6e90\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<h4>7.3 \u4f7f\u7528<code>contextlib.suppress<\/code><\/h4>\n<p><code>contextlib.suppress<\/code>\u662fPython 3.4+\u4e2d\u5f15\u5165\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u5ffd\u7565\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/p>\n<h4>7.4 \u4f7f\u7528<code>traceback<\/code>\u6a21\u5757<\/h4>\n<p><code>traceback<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u7528\u4e8e\u5904\u7406\u548c\u683c\u5f0f\u5316\u5f02\u5e38\u7684\u5806\u6808\u8ddf\u8e2a\u4fe1\u606f\u3002<\/p>\n<h4>7.5 \u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u4f18\u5316<\/h4>\n<p>\u5f02\u5e38\u5904\u7406\u4f1a\u5bf9\u7a0b\u5e8f\u7684\u6027\u80fd\u4ea7\u751f\u4e00\u5b9a\u7684\u5f71\u54cd\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u6765\u4f18\u5316\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\uff1a<\/p>\n<ul>\n<li><strong>\u907f\u514d\u5728\u70ed\u70b9\u8def\u5f84\u4e2d\u4f7f\u7528\u5f02\u5e38<\/strong>\uff1a\u907f\u514d\u5728\u9891\u7e41\u6267\u884c\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u5f02\u5e38<\/li>\n<li><strong>\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf<\/strong>\uff1a\u5728\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u4e2d\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u5168\u5c40\u53d8\u91cf<\/li>\n<li><strong>\u4fdd\u6301\u5f02\u5e38\u5904\u7406\u7684\u7b80\u6d01<\/strong>\uff1a\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u5e94\u8be5\u4fdd\u6301\u7b80\u6d01\uff0c\u53ea\u5305\u542b\u5fc5\u8981\u7684\u4ee3\u7801<\/li>\n<li><strong>\u4f7f\u7528EAFP\u6a21\u5f0f<\/strong>\uff1a\u5728\u9002\u5f53\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528EAFP\u6a21\u5f0f\uff0c\u800c\u4e0d\u662fLBYL\u6a21\u5f0f<\/li>\n<\/ul>\n<pre><code class=\"python\"># \u5f02\u5e38\u5904\u7406\u7684\u9ad8\u7ea7\u6280\u5de7\u793a\u4f8b\n\n# 1. \u4f7f\u7528\u88c5\u9970\u5668\u5904\u7406\u5f02\u5e38\nprint(&quot;\u4f7f\u7528\u88c5\u9970\u5668\u5904\u7406\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\nimport functools\n\ndef handle_exceptions(default=None, log=True):\n    &quot;&quot;&quot;\u5f02\u5e38\u5904\u7406\u88c5\u9970\u5668&quot;&quot;&quot;\n    def decorator(func):\n        @functools.wraps(func)\n        def wrapper(*args, **kwargs):\n            try:\n                return func(*args, **kwargs)\n            except Exception as e:\n                if log:\n                    print(f&quot;\u51fd\u6570 {func.__name__} \u53d1\u751f\u5f02\u5e38: {e}&quot;)\n                return default\n        return wrapper\n    return decorator\n\n@handle_exceptions(default=&quot;\u9519\u8bef&quot;, log=True)\ndef risky_operation():\n    &quot;&quot;&quot; risky operation &quot;&quot;&quot;\n    10 \/ 0\n\nresult = risky_operation()\nprint(f&quot;\u7ed3\u679c: {result}&quot;)\n\n@handle_exceptions(default=[], log=False)\ndef parse_json(json_str):\n    &quot;&quot;&quot;\u89e3\u6790JSON\u5b57\u7b26\u4e32&quot;&quot;&quot;\n    import json\n    return json.loads(json_str)\n\nresult = parse_json(&quot;invalid json&quot;)\nprint(f&quot;\u89e3\u6790\u7ed3\u679c: {result}&quot;)\n\n# 2. \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5904\u7406\u5f02\u5e38\nprint(&quot;\/n\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5904\u7406\u5f02\u5e38\u793a\u4f8b\uff1a&quot;)\n\nclass ExceptionHandler:\n    &quot;&quot;&quot;\u5f02\u5e38\u5904\u7406\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&quot;&quot;&quot;\n    def __init__(self, default=None, *exceptions):\n        self.default = default\n        self.exceptions = exceptions or (Exception,)\n    \n    def __enter__(self):\n        return self\n    \n    def __exit__(self, exc_type, exc_val, exc_tb):\n        if exc_type is not None and issubclass(exc_type, self.exceptions):\n            print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {exc_val}&quot;)\n            return True  # \u6291\u5236\u5f02\u5e38\n        return False  # \u4e0d\u6291\u5236\u5f02\u5e38\n\nwith ExceptionHandler(default=&quot;\u9519\u8bef&quot;, ZeroDivisionError, ValueError) as handler:\n    result = 10 \/ 0\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\n\nprint(&quot;\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6267\u884c\u5b8c\u6bd5&quot;)\n\n# 3. \u4f7f\u7528contextlib.suppress\nprint(&quot;\/n\u4f7f\u7528contextlib.suppress\u793a\u4f8b\uff1a&quot;)\n\nfrom contextlib import suppress\n\n# \u5ffd\u7565\u7279\u5b9a\u5f02\u5e38\nwith suppress(ZeroDivisionError):\n    result = 10 \/ 0\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nprint(&quot;\u64cd\u4f5c\u5b8c\u6210&quot;)\n\n# \u5ffd\u7565\u591a\u4e2a\u5f02\u5e38\nwith suppress(ZeroDivisionError, ValueError):\n    result = int(&quot;abc&quot;)\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nprint(&quot;\u64cd\u4f5c\u5b8c\u6210&quot;)\n\n# 4. \u4f7f\u7528traceback\u6a21\u5757\nprint(&quot;\/n\u4f7f\u7528traceback\u6a21\u5757\u793a\u4f8b\uff1a&quot;)\n\nimport traceback\n\ndef nested_function():\n    &quot;&quot;&quot;\u5d4c\u5957\u51fd\u6570&quot;&quot;&quot;\n    10 \/ 0\n\ndef outer_function():\n    &quot;&quot;&quot;\u5916\u90e8\u51fd\u6570&quot;&quot;&quot;\n    nested_function()\n\ntry:\n    outer_function()\nexcept Exception as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)\n    print(&quot;\/n\u5806\u6808\u8ddf\u8e2a:&quot;)\n    traceback.print_exc()\n    \n    # \u83b7\u53d6\u5806\u6808\u8ddf\u8e2a\u4fe1\u606f\u4f5c\u4e3a\u5b57\u7b26\u4e32\n    traceback_str = traceback.format_exc()\n    print(&quot;\/n\u5806\u6808\u8ddf\u8e2a\u5b57\u7b26\u4e32:&quot;)\n    print(traceback_str)\n\n# 5. \u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u4f18\u5316\nprint(&quot;\/n\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u4f18\u5316\u793a\u4f8b\uff1a&quot;)\n\nimport time\n\n# \u6d4b\u8bd5EAFP\u6a21\u5f0f\u7684\u6027\u80fd\ndef eafp_approach(dictionary, key):\n    &quot;&quot;&quot;\u4f7f\u7528EAFP\u6a21\u5f0f&quot;&quot;&quot;\n    try:\n        return dictionary[key]\n    except KeyError:\n        return &quot;\u9ed8\u8ba4\u503c&quot;\n\n# \u6d4b\u8bd5LBYL\u6a21\u5f0f\u7684\u6027\u80fd\ndef lbyl_approach(dictionary, key):\n    &quot;&quot;&quot;\u4f7f\u7528LBYL\u6a21\u5f0f&quot;&quot;&quot;\n    if key in dictionary:\n        return dictionary[key]\n    else:\n        return &quot;\u9ed8\u8ba4\u503c&quot;\n\n# \u6d4b\u8bd5\u6027\u80fd\ndictionary = {f&quot;key{i}&quot;: i for i in range(1000)}\n\n# \u6d4b\u8bd5\u952e\u5b58\u5728\u7684\u60c5\u51b5\nprint(&quot;\u6d4b\u8bd5\u952e\u5b58\u5728\u7684\u60c5\u51b5\uff1a&quot;)\nstart = time.time()\nfor i in range(1000000):\n    eafp_approach(dictionary, &quot;key500&quot;)\nend = time.time()\nprint(f&quot;EAFP\u6a21\u5f0f\u8017\u65f6: {end - start:.4f}\u79d2&quot;)\n\nstart = time.time()\nfor i in range(1000000):\n    lbyl_approach(dictionary, &quot;key500&quot;)\nend = time.time()\nprint(f&quot;LBYL\u6a21\u5f0f\u8017\u65f6: {end - start:.4f}\u79d2&quot;)\n\n# \u6d4b\u8bd5\u952e\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\nprint(&quot;\/n\u6d4b\u8bd5\u952e\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\uff1a&quot;)\nstart = time.time()\nfor i in range(100000):\n    eafp_approach(dictionary, &quot;key1000&quot;)\nend = time.time()\nprint(f&quot;EAFP\u6a21\u5f0f\u8017\u65f6: {end - start:.4f}\u79d2&quot;)\n\nstart = time.time()\nfor i in range(100000):\n    lbyl_approach(dictionary, &quot;key1000&quot;)\nend = time.time()\nprint(f&quot;LBYL\u6a21\u5f0f\u8017\u65f6: {end - start:.4f}\u79d2&quot;)\n\n# 6. \u4f7f\u7528functools.wraps\u4fdd\u7559\u51fd\u6570\u5143\u6570\u636e\nprint(&quot;\/n\u4f7f\u7528functools.wraps\u4fdd\u7559\u51fd\u6570\u5143\u6570\u636e\u793a\u4f8b\uff1a&quot;)\n\ndef error_handler(func):\n    &quot;&quot;&quot;\u9519\u8bef\u5904\u7406\u88c5\u9970\u5668&quot;&quot;&quot;\n    @functools.wraps(func)\n    def wrapper(*args, **kwargs):\n        try:\n            return func(*args, **kwargs)\n        except Exception as e:\n            print(f&quot;\u9519\u8bef: {e}&quot;)\n            raise\n    return wrapper\n\n@error_handler\ndef calculate(a, b):\n    &quot;&quot;&quot;\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c&quot;&quot;&quot;\n    return a + b\n\nprint(f&quot;\u51fd\u6570\u540d: {calculate.__name__}&quot;)\nprint(f&quot;\u51fd\u6570\u6587\u6863: {calculate.__doc__}&quot;)\nprint(f&quot;\u51fd\u6570\u53c2\u6570: {calculate.__code__.co_varnames}&quot;)\n\ntry:\n    result = calculate(10, &quot;20&quot;)\n    print(f&quot;\u7ed3\u679c: {result}&quot;)\nexcept Exception as e:\n    print(f&quot;\u6355\u83b7\u5230\u5f02\u5e38: {e}&quot;)<\/code><\/pre>\n<h3>8. \u5e38\u89c1\u5f02\u5e38\u5904\u7406\u573a\u666f<\/h3>\n<h4>8.1 \u6587\u4ef6\u64cd\u4f5c<\/h4>\n<p>\u6587\u4ef6\u64cd\u4f5c\u662fPython\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u5f02\u5e38\u5904\u7406\u573a\u666f\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u9519\u8bef\u7b49\u5f02\u5e38\u3002<\/p>\n<h4>8.2 \u7f51\u7edc\u64cd\u4f5c<\/h4>\n<p>\u7f51\u7edc\u64cd\u4f5c\u662f\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u5f02\u5e38\u5904\u7406\u573a\u666f\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u8fde\u63a5\u9519\u8bef\u3001\u8d85\u65f6\u9519\u8bef\u7b49\u5f02\u5e38\u3002<\/p>\n<h4>8.3 \u6570\u636e\u5e93\u64cd\u4f5c<\/h4>\n<p>\u6570\u636e\u5e93\u64cd\u4f5c\u662fPython\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u5f02\u5e38\u5904\u7406\u573a\u666f\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u8fde\u63a5\u9519\u8bef\u3001\u67e5\u8be2\u9519\u8bef\u7b49\u5f02\u5e38\u3002<\/p>\n<h4>8.4 API\u8c03\u7528<\/h4>\n<p>API\u8c03\u7528\u662fPython\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u5f02\u5e38\u5904\u7406\u573a\u666f\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u7f51\u7edc\u9519\u8bef\u3001API\u9519\u8bef\u7b49\u5f02\u5e38\u3002<\/p>\n<h4>8.5 \u8f93\u5165\u9a8c\u8bc1<\/h4>\n<p>\u8f93\u5165\u9a8c\u8bc1\u662fPython\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u5f02\u5e38\u5904\u7406\u573a\u666f\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u65e0\u6548\u8f93\u5165\u3001\u7c7b\u578b\u9519\u8bef\u7b49\u5f02\u5e38\u3002<\/p>\n<pre><code class=\"python\"># \u5e38\u89c1\u5f02\u5e38\u5904\u7406\u573a\u666f\u793a\u4f8b\n\n# 1. \u6587\u4ef6\u64cd\u4f5c\nprint(&quot;\u6587\u4ef6\u64cd\u4f5c\u793a\u4f8b\uff1a&quot;)\n\ndef read_file_safely(filename):\n    &quot;&quot;&quot;\u5b89\u5168\u5730\u8bfb\u53d6\u6587\u4ef6&quot;&quot;&quot;\n    try:\n        with open(filename, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:\n            content = f.read()\n        return content\n    except FileNotFoundError:\n        print(f&quot;\u9519\u8bef\uff1a\u6587\u4ef6 {filename} \u4e0d\u5b58\u5728&quot;)\n        return &quot;&quot;\n    except PermissionError:\n        print(f&quot;\u9519\u8bef\uff1a\u6ca1\u6709\u8bfb\u53d6\u6587\u4ef6 {filename} \u7684\u6743\u9650&quot;)\n        return &quot;&quot;\n    except UnicodeDecodeError:\n        print(f&quot;\u9519\u8bef\uff1a\u6587\u4ef6 {filename} \u7f16\u7801\u9519\u8bef&quot;)\n        return &quot;&quot;\n    except Exception as e:\n        print(f&quot;\u9519\u8bef\uff1a\u8bfb\u53d6\u6587\u4ef6\u65f6\u53d1\u751f\u672a\u77e5\u9519\u8bef: {e}&quot;)\n        return &quot;&quot;\n\ncontent = read_file_safely(&quot;non_existent_file.txt&quot;)\nprint(f&quot;\u6587\u4ef6\u5185\u5bb9\u957f\u5ea6: {len(content)}&quot;)\n\n# 2. \u7f51\u7edc\u64cd\u4f5c\nprint(&quot;\/n\u7f51\u7edc\u64cd\u4f5c\u793a\u4f8b\uff1a&quot;)\n\ndef fetch_url(url, timeout=10):\n    &quot;&quot;&quot;\u83b7\u53d6URL\u5185\u5bb9&quot;&quot;&quot;\n    import requests\n    try:\n        response = requests.get(url, timeout=timeout)\n        response.raise_for_status()  # \u5f15\u53d1HTTP\u9519\u8bef\n        return response.text\n    except requests.exceptions.ConnectionError:\n        print(f&quot;\u9519\u8bef\uff1a\u65e0\u6cd5\u8fde\u63a5\u5230 {url}&quot;)\n        return &quot;&quot;\n    except requests.exceptions.Timeout:\n        print(f&quot;\u9519\u8bef\uff1a\u8bf7\u6c42 {url} \u8d85\u65f6&quot;)\n        return &quot;&quot;\n    except requests.exceptions.HTTPError as e:\n        print(f&quot;\u9519\u8bef\uff1aHTTP\u9519\u8bef: {e}&quot;)\n        return &quot;&quot;\n    except Exception as e:\n        print(f&quot;\u9519\u8bef\uff1a\u53d1\u751f\u672a\u77e5\u9519\u8bef: {e}&quot;)\n        return &quot;&quot;\n\n# \u6d4b\u8bd5\u7f51\u7edc\u64cd\u4f5c\n# content = fetch_url(&quot;https:\/\/example.com&quot;)\n# print(f&quot;URL\u5185\u5bb9\u957f\u5ea6: {len(content)}&quot;)\n\n# 3. \u6570\u636e\u5e93\u64cd\u4f5c\nprint(&quot;\/n\u6570\u636e\u5e93\u64cd\u4f5c\u793a\u4f8b\uff1a&quot;)\n\ndef query_database(query, params=None):\n    &quot;&quot;&quot;\u67e5\u8be2\u6570\u636e\u5e93&quot;&quot;&quot;\n    import sqlite3\n    try:\n        conn = sqlite3.connect(&quot;:memory:&quot;)\n        cursor = conn.cursor()\n        cursor.execute(query, params or ())\n        results = cursor.fetchall()\n        conn.commit()\n        return results\n    except sqlite3.Error as e:\n        print(f&quot;\u6570\u636e\u5e93\u9519\u8bef: {e}&quot;)\n        return []\n    finally:\n        if 'conn' in locals():\n            conn.close()\n\n# \u6d4b\u8bd5\u6570\u636e\u5e93\u64cd\u4f5c\nresults = query_database(&quot;CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)&quot;)\nprint(f&quot;\u521b\u5efa\u8868\u7ed3\u679c: {results}&quot;)\n\nresults = query_database(&quot;INSERT INTO users (name) VALUES (?)&quot;, (&quot;John&quot;,))\nprint(f&quot;\u63d2\u5165\u7ed3\u679c: {results}&quot;)\n\nresults = query_database(&quot;SELECT * FROM users&quot;)\nprint(f&quot;\u67e5\u8be2\u7ed3\u679c: {results}&quot;)\n\n# 4. API\u8c03\u7528\nprint(&quot;\/nAPI\u8c03\u7528\u793a\u4f8b\uff1a&quot;)\n\nclass APIError(Exception):\n    &quot;&quot;&quot;API\u9519\u8bef&quot;&quot;&quot;\n    pass\n\ndef call_api(endpoint, method=&quot;GET&quot;, data=None):\n    &quot;&quot;&quot;\u8c03\u7528API&quot;&quot;&quot;\n    import requests\n    base_url = &quot;https:\/\/api.example.com&quot;\n    url = f&quot;{base_url}{endpoint}&quot;\n    \n    try:\n        if method == &quot;GET&quot;:\n            response = requests.get(url, params=data)\n        elif method == &quot;POST&quot;:\n            response = requests.post(url, json=data)\n        else:\n            raise APIError(f&quot;\u4e0d\u652f\u6301\u7684HTTP\u65b9\u6cd5: {method}&quot;)\n        \n        response.raise_for_status()\n        return response.json()\n    except requests.exceptions.RequestException as e:\n        raise APIError(f&quot;\u7f51\u7edc\u9519\u8bef: {e}&quot;)\n    except ValueError:\n        raise APIError(&quot;API\u8fd4\u56de\u7684\u4e0d\u662f\u6709\u6548\u7684JSON&quot;)\n    except Exception as e:\n        raise APIError(f&quot;\u672a\u77e5\u9519\u8bef: {e}&quot;)\n\n# \u6d4b\u8bd5API\u8c03\u7528\n# try:\n#     result = call_api(&quot;\/users&quot;, method=&quot;GET&quot;, data={&quot;page&quot;: 1})\n#     print(f&quot;API\u8c03\u7528\u7ed3\u679c: {result}&quot;)\n# except APIError as e:\n#     print(f&quot;\u6355\u83b7\u5230API\u9519\u8bef: {e}&quot;)\n\n# 5. \u8f93\u5165\u9a8c\u8bc1\nprint(&quot;\/n\u8f93\u5165\u9a8c\u8bc1\u793a\u4f8b\uff1a&quot;)\n\nclass ValidationError(Exception):\n    &quot;&quot;&quot;\u9a8c\u8bc1\u9519\u8bef&quot;&quot;&quot;\n    pass\n\ndef validate_input(data):\n    &quot;&quot;&quot;\u9a8c\u8bc1\u8f93\u5165\u6570\u636e&quot;&quot;&quot;\n    if not isinstance(data, dict):\n        raise ValidationError(&quot;\u8f93\u5165\u5fc5\u987b\u662f\u5b57\u5178&quot;)\n    \n    required_fields = [&quot;name&quot;, &quot;email&quot;, &quot;age&quot;]\n    for field in required_fields:\n        if field not in data:\n            raise ValidationError(f&quot;\u7f3a\u5c11\u5fc5\u586b\u5b57\u6bb5: {field}&quot;)\n    \n    if not isinstance(data[&quot;name&quot;], str) or not data[&quot;name&quot;]:\n        raise ValidationError(&quot;\u59d3\u540d\u5fc5\u987b\u662f\u975e\u7a7a\u5b57\u7b26\u4e32&quot;)\n    \n    if not isinstance(data[&quot;email&quot;], str) or &quot;@&quot; not in data[&quot;email&quot;]:\n        raise ValidationError(&quot;\u90ae\u7bb1\u683c\u5f0f\u65e0\u6548&quot;)\n    \n    if not isinstance(data[&quot;age&quot;], int) or data[&quot;age&quot;] &lt; 0:\n        raise ValidationError(&quot;\u5e74\u9f84\u5fc5\u987b\u662f\u975e\u8d1f\u6574\u6570&quot;)\n    \n    return True\n\ntry:\n    user_data = {\n        &quot;name&quot;: &quot;John&quot;,\n        &quot;email&quot;: &quot;john@example.com&quot;,\n        &quot;age&quot;: 30\n    }\n    validate_input(user_data)\n    print(&quot;\u8f93\u5165\u9a8c\u8bc1\u6210\u529f&quot;)\nexcept ValidationError as e:\n    print(f&quot;\u9a8c\u8bc1\u9519\u8bef: {e}&quot;)\n\ntry:\n    user_data = {\n        &quot;name&quot;: &quot;&quot;,\n        &quot;email&quot;: &quot;invalid-email&quot;,\n        &quot;age&quot;: -5\n    }\n    validate_input(user_data)\n    print(&quot;\u8f93\u5165\u9a8c\u8bc1\u6210\u529f&quot;)\nexcept ValidationError as e:\n    print(f&quot;\u9a8c\u8bc1\u9519\u8bef: {e}&quot;)<\/code><\/pre>\n<h3>9. \u603b\u7ed3<\/h3>\n<p>\u672c\u6587\u8be6\u7ec6\u5206\u6790\u4e86Python\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u4e0e\u6700\u4f73\u5b9e\u8df5\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5<\/strong>\uff1a\u5f02\u5e38\u7684\u5b9a\u4e49\u3001\u5f02\u5e38\u4e0e\u9519\u8bef\u7684\u533a\u522b\u3001\u5f02\u5e38\u7684\u5c42\u6b21\u7ed3\u6784\u3001\u5e38\u89c1\u7684\u5185\u7f6e\u5f02\u5e38<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406\u673a\u5236<\/strong>\uff1a\u5f02\u5e38\u5904\u7406\u7684\u57fa\u672c\u8bed\u6cd5\u3001try-except\u8bed\u53e5\u3001\u5f02\u5e38\u7684\u4f20\u9012<\/li>\n<li><strong>\u5f02\u5e38\u7684\u5f15\u53d1\u4e0e\u4f20\u64ad<\/strong>\uff1araise\u8bed\u53e5\u3001\u91cd\u65b0\u5f15\u53d1\u5f02\u5e38\u3001\u5f02\u5e38\u7684\u4f20\u64ad\u3001\u5f02\u5e38\u94fe<\/li>\n<li><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong>\uff1a\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u6700\u4f73\u5b9e\u8df5\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u5e94\u7528\u573a\u666f<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u5f02\u5e38\u5904\u7406\u7684\u539f\u5219\u3001\u5e38\u89c1\u9519\u8bef\u3001EAFP\u6a21\u5f0fvs LBYL\u6a21\u5f0f\u3001\u6027\u80fd\u8003\u8651<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406\u4e0e\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u65e5\u5fd7\u8bb0\u5f55\u7684\u57fa\u672c\u6982\u5ff5\u3001\u5f02\u5e38\u5904\u7406\u4e0e\u65e5\u5fd7\u8bb0\u5f55\u7684\u7ed3\u5408\u3001\u65e5\u5fd7\u8bb0\u5f55\u7684\u914d\u7f6e<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406\u4e0e\u6d4b\u8bd5<\/strong>\uff1a\u6d4b\u8bd5\u5f02\u5e38\u7684\u57fa\u672c\u65b9\u6cd5\u3001\u6d4b\u8bd5\u5f02\u5e38\u7684\u6700\u4f73\u5b9e\u8df5\u3001\u5f02\u5e38\u5904\u7406\u7684\u6d4b\u8bd5\u793a\u4f8b<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406\u7684\u9ad8\u7ea7\u6280\u5de7<\/strong>\uff1a\u4f7f\u7528\u88c5\u9970\u5668\u5904\u7406\u5f02\u5e38\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5904\u7406\u5f02\u5e38\u3001\u4f7f\u7528contextlib.suppress\u3001\u4f7f\u7528traceback\u6a21\u5757\u3001\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u4f18\u5316<\/li>\n<li><strong>\u5e38\u89c1\u5f02\u5e38\u5904\u7406\u573a\u666f<\/strong>\uff1a\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u3001API\u8c03\u7528\u3001\u8f93\u5165\u9a8c\u8bc1<\/li>\n<\/ul>\n<p>Python\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u9519\u8bef\u5904\u7406\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u6355\u83b7\u548c\u5904\u7406\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u4ece\u800c\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ee\u548c\u53ef\u9760\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u5e94\u8be5\u80fd\u591f\uff1a<\/p>\n<ol>\n<li>\u7406\u89e3Python\u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5\u548c\u5c42\u6b21\u7ed3\u6784<\/li>\n<li>\u638c\u63e1Python\u5f02\u5e38\u5904\u7406\u7684\u57fa\u672c\u8bed\u6cd5\u548c\u673a\u5236<\/li>\n<li>\u5b66\u4f1a\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38<\/li>\n<li>\u9075\u5faaPython\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/li>\n<li>\u7ed3\u5408\u65e5\u5fd7\u8bb0\u5f55\u548c\u6d4b\u8bd5\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027<\/li>\n<li>\u5e94\u7528\u5f02\u5e38\u5904\u7406\u6280\u5de7\u89e3\u51b3\u5b9e\u9645\u95ee\u9898<\/li>\n<\/ol>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u5e94\u8be5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u5f02\u5e38\u5904\u7406\u7b56\u7565\uff0c\u9075\u5faaPython\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u540c\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\u4fdd\u6301\u5b66\u4e60\u7684\u6001\u5ea6\uff0c\u5173\u6ce8Python\u7684\u6700\u65b0\u53d1\u5c55\uff0c\u4ee5\u5145\u5206\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<h3>10. \u53c2\u8003\u6587\u732e<\/h3>\n<ol>\n<li>Python Documentation: Errors and Exceptions<\/li>\n<li>Python Documentation: Built-in Exceptions<\/li>\n<li>Python Documentation: logging &#8211; Logging facility for Python<\/li>\n<li>Python Documentation: contextlib &#8211; Utilities for with-statement contexts<\/li>\n<li>Python Documentation: traceback &#8211; Print or retrieve a stack traceback<\/li>\n<li>PEP 8 &#8212; Style Guide for Python Code<\/li>\n<li>PEP 3134 &#8212; Exception Chaining and Embedded Tracebacks<\/li>\n<li>Real Python: Python Exceptions: An Introduction<\/li>\n<li>Real Python: Logging in Python<\/li>\n<li>Real Python: Testing Your Code With pytest<\/li>\n<\/ol>\n<h3>11. \u7ed3\u8bed<\/h3>\n<p>Python\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u662fPython\u8bed\u8a00\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u800c\u5f3a\u5927\u7684\u9519\u8bef\u5904\u7406\u65b9\u5f0f\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u66f4\u52a0\u5065\u58ee\u3001\u53ef\u9760\u548c\u53ef\u7ef4\u62a4\u7684Python\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\uff1a<\/p>\n<ul>\n<li><strong>\u6b63\u786e\u7406\u89e3\u5f02\u5e38<\/strong>\uff1a\u7406\u89e3\u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5\u548c\u5c42\u6b21\u7ed3\u6784<\/li>\n<li><strong>\u5408\u7406\u4f7f\u7528\u5f02\u5e38<\/strong>\uff1a\u53ea\u5728\u5fc5\u8981\u65f6\u4f7f\u7528\u5f02\u5e38\uff0c\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u5f02\u5e38<\/li>\n<li><strong>\u6355\u83b7\u5fc5\u8981\u7684\u5f02\u5e38<\/strong>\uff1a\u53ea\u6355\u83b7\u80fd\u591f\u5904\u7406\u7684\u5f02\u5e38\uff0c\u4f7f\u7528\u5177\u4f53\u7684\u5f02\u5e38\u7c7b\u578b<\/li>\n<li><strong>\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u5f02\u5e38\u4fe1\u606f<\/strong>\uff1a\u5728\u5f02\u5e38\u4fe1\u606f\u4e2d\u5305\u542b\u8db3\u591f\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f<\/li>\n<li><strong>\u53ca\u65f6\u91ca\u653e\u8d44\u6e90<\/strong>\uff1a\u4f7f\u7528finally\u5b50\u53e5\u6216\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u786e\u4fdd\u8d44\u6e90\u7684\u91ca\u653e<\/li>\n<li><strong>\u7ed3\u5408\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u6765\u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u95ee\u9898\u6392\u67e5<\/li>\n<li><strong>\u6d4b\u8bd5\u5f02\u5e38\u5904\u7406<\/strong>\uff1a\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u6765\u6d4b\u8bd5\u5f02\u5e38\u5904\u7406\u4ee3\u7801\uff0c\u786e\u4fdd\u5176\u6b63\u786e\u6027<\/li>\n<li><strong>\u4e0d\u65ad\u5b66\u4e60<\/strong>\uff1a\u5173\u6ce8Python\u7684\u6700\u65b0\u53d1\u5c55\uff0c\u5b66\u4e60\u65b0\u7684\u5f02\u5e38\u5904\u7406\u6280\u5de7\u548c\u6700\u4f73\u5b9e\u8df5<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u539f\u5219\uff0c\u6211\u4eec\u53ef\u4ee5\u5145\u5206\u5229\u7528Python\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u7f16\u5199\u66f4\u52a0\u5065\u58ee\u3001\u53ef\u9760\u548c\u53ef\u7ef4\u62a4\u7684Python\u4ee3\u7801\u3002\u5f02\u5e38\u5904\u7406\u4e0d\u4ec5\u662f\u4e00\u79cd\u9519\u8bef\u5904\u7406\u65b9\u5f0f\uff0c\u66f4\u662f\u4e00\u79cd\u7f16\u7a0b\u601d\u60f3\uff0c\u5b83\u4f53\u73b0\u4e86Python\u8bed\u8a00\u7684\u4f18\u96c5\u548c\u5f3a\u5927\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3Python\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u638c\u63e1\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u4ece\u800c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7f16\u5199\u51fa\u66f4\u9ad8\u8d28\u91cf\u7684Python\u4ee3\u7801\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047585258\">https:\/\/segmentfault.com\/a\/1190000047585258<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u4e0e\u6700\u4f73\u5b9e\u8df5 1. \u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5 \u5728Python\u7f16\u7a0b\u4e2d\uff0c\u5f02\u5e38\u662f\u6307\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u9047\u5230 [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021229","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021229"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021229\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}