{"id":16479,"date":"2024-11-05T14:06:04","date_gmt":"2024-11-05T06:06:04","guid":{"rendered":"https:\/\/92it.top\/?p=16479"},"modified":"2024-11-05T14:06:04","modified_gmt":"2024-11-05T06:06:04","slug":"try-catch-%e4%bc%9a%e5%bd%b1%e5%93%8d%e6%80%a7%e8%83%bd%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16479","title":{"rendered":"try-catch \u4f1a\u5f71\u54cd\u6027\u80fd\u5417\uff1f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<strong><a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247500183&amp;idx=1&amp;sn=c92388f7f3f4baa99b91ff0adc6fbf74&amp;chksm=96c4edaba1b364bd6a4b89f708422101144ed9a05ece0d570eb58fd3dfcb6fc1924799921080&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">try-catch \u4f1a\u5f71\u54cd\u6027\u80fd\u5417\uff1f<\/a><\/strong><\/p>\n\n\n\n<p>\u5728\u7f16\u7a0b\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u6355\u83b7\u548c\u5904\u7406\u9519\u8bef\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u5730\u5d29\u6e83\u3002\u5728\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u5305\u62ecJava\u3001C++\u3001C#\u548cPython\uff0c<code>try-catch<\/code>\u7ed3\u6784\u662f\u5b9e\u73b0\u8fd9\u79cd\u5f02\u5e38\u5904\u7406\u7684\u5e38\u7528\u673a\u5236\u3002\u90a3\u4e48\uff0c<code>try-catch<\/code>\u662f\u5426\u4f1a\u5f71\u54cd\u6027\u80fd\uff1f\u8fd9\u7bc7\u6587\u7ae0\u6211\u4eec\u6765\u804a\u4e00\u804a\u3002<\/p>\n\n\n\n<p><strong>\u5f02\u5e38\u5904\u7406\u7684\u57fa\u672c\u539f\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u4e86\u89e3<code>try-catch<\/code>\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u4e86\u89e3\u5f02\u5e38\u5904\u7406\u7684\u57fa\u672c\u539f\u7406\u3002\u5728\u5927\u591a\u6570\u73b0\u4ee3\u8bed\u8a00\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u901a\u8fc7\u4e00\u79cd\u79f0\u4e3a\u201c\u6808\u5c55\u5f00\u201d\uff08stack unwinding\uff09\u7684\u673a\u5236\u6765\u5b9e\u73b0\u7684\u3002\u5f53\u7a0b\u5e8f\u8fd0\u884c\u5230<code>try<\/code>\u5757\u5e76\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u8df3\u8f6c\u5230\u76f8\u5e94\u7684<code>catch<\/code>\u5757\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u79cd\u8df3\u8f6c\u901a\u5e38\u6d89\u53ca\u5230\u8c03\u7528\u6808\u7684\u904d\u5386\u548c\u8c03\u6574\uff0c\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u5e26\u6765\u6027\u80fd\u5f00\u9500\u3002<\/p>\n\n\n\n<p><strong><code>try-catch<\/code>\u5bf9\u6027\u80fd\u7684\u5f71\u54cd<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>1. \u7f16\u8bd1\u5668\u548c\u8fd0\u884c\u65f6\u7684\u4f18\u5316<\/strong><\/p>\n\n\n\n<p>\u8bb8\u591a\u73b0\u4ee3\u7f16\u8bd1\u5668\u548c\u8fd0\u884c\u65f6\u73af\u5883\u90fd\u5bf9\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u4e86\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u5728Java\u4e2d\uff0cHotSpot JVM\u5bf9\u5f02\u5e38\u5904\u7406\u7684\u5f00\u9500\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u4f7f\u5f97\u5728\u6ca1\u6709\u5f02\u5e38\u53d1\u751f\u7684\u60c5\u51b5\u4e0b\uff0c<code>try-catch<\/code>\u5757\u7684\u5f00\u9500\u51e0\u4e4e\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\u3002JIT\uff08Just-In-Time\uff09\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u4f18\u5316\u4ee3\u7801\u8def\u5f84\uff0c\u4f7f\u5f97\u6b63\u5e38\u6267\u884c\u8def\u5f84\u4e0d\u53d7\u989d\u5916\u7684\u5f02\u5e38\u5904\u7406\u903b\u8f91\u5f71\u54cd\u3002<\/p>\n\n\n\n<p><strong>2. \u5f02\u5e38\u7684\u4ee3\u4ef7<\/strong><\/p>\n\n\n\n<p>\u5c3d\u7ba1<code>try-catch<\/code>\u5757\u672c\u8eab\u5728\u6ca1\u6709\u5f02\u5e38\u53d1\u751f\u65f6\u5f00\u9500\u5f88\u5c0f\uff0c\u4f46\u4e00\u65e6\u53d1\u751f\u5f02\u5e38\uff0c\u6027\u80fd\u5f71\u54cd\u5c31\u4f1a\u663e\u8457\u589e\u52a0\u3002\u8fd9\u662f\u56e0\u4e3a\u5f02\u5e38\u5904\u7406\u6d89\u53ca\u5230\u6808\u5c55\u5f00\u3001\u5bf9\u8c61\u521b\u5efa\uff08\u5f02\u5e38\u5bf9\u8c61\u901a\u5e38\u662f\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff09\u4ee5\u53ca\u53ef\u80fd\u7684\u5783\u573e\u56de\u6536\u7b49\u64cd\u4f5c\u3002\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u6bd4\u666e\u901a\u7684\u51fd\u6570\u8c03\u7528\u8981\u6602\u8d35\u5f97\u591a\u3002\u56e0\u6b64\uff0c\u5728\u6027\u80fd\u654f\u611f\u7684\u4ee3\u7801\u4e2d\uff0c\u9891\u7e41\u629b\u51fa\u548c\u6355\u83b7\u5f02\u5e38\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u74f6\u9888\u3002<\/p>\n\n\n\n<p><strong>3. \u4e0d\u540c\u8bed\u8a00\u7684\u5dee\u5f02<\/strong><\/p>\n\n\n\n<p>\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u5bf9\u5f02\u5e38\u5904\u7406\u673a\u5236\u7684\u5b9e\u73b0\u6709\u6240\u4e0d\u540c\uff0c\u56e0\u6b64<code>try-catch<\/code>\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u4e5f\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u5728C++\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u4f7f\u7528\u7684\u662f\u96f6\u5f00\u9500\u6a21\u578b\uff08zero-cost model\uff09\uff0c\u5373\u5728\u6ca1\u6709\u5f02\u5e38\u53d1\u751f\u65f6\uff0c\u4e0d\u4f1a\u5e26\u6765\u989d\u5916\u7684\u6027\u80fd\u5f00\u9500\u3002\u7136\u800c\uff0c\u4e00\u65e6\u53d1\u751f\u5f02\u5e38\uff0c\u4ee3\u4ef7\u5c31\u4f1a\u6bd4\u8f83\u9ad8\u3002\u5728Python\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u673a\u5236\u76f8\u5bf9\u7b80\u5355\uff0c\u4f46\u7531\u4e8ePython\u672c\u8eab\u662f\u4e00\u79cd\u89e3\u91ca\u578b\u8bed\u8a00\uff0c\u5f02\u5e38\u5904\u7406\u7684\u5f00\u9500\u76f8\u5bf9\u8f83\u9ad8\u3002<\/p>\n\n\n\n<p><strong>Java\u4e2d\u7684try-catch<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Java\u4e2d\uff0c<code>try-catch<\/code>\u5757\u662f\u5f02\u5e38\u5904\u7406\u7684\u91cd\u8981\u673a\u5236\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u8981\u7406\u89e3\u5176\u5b9e\u73b0\u539f\u7406\uff0c\u6211\u4eec\u9700\u8981\u4ece Java\u8bed\u8a00\u89c4\u8303\u3001\u5b57\u8282\u7801\u751f\u6210\u4ee5\u53ca Java\u865a\u62df\u673a\uff08JVM\uff09\u7684\u89d2\u5ea6\u6765\u8fdb\u884c\u5206\u6790\u3002<\/p>\n\n\n\n<p><strong>Java\u8bed\u8a00\u5c42\u9762\u7684\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n\n\n\n<p>\u5728 Java\u4e2d\uff0c\u5f02\u5e38\u662f<code>Throwable<\/code>\u7c7b\u7684\u5b9e\u4f8b\uff0c<code>Throwable<\/code>\u662f\u6240\u6709\u9519\u8bef\u548c\u5f02\u5e38\u7684\u8d85\u7c7b\u3002Java\u63d0\u4f9b\u4e86\u4e24\u79cd\u5f02\u5e38\u7c7b\u578b\uff1a\u5df2\u68c0\u67e5\u5f02\u5e38\uff08checked exceptions\uff09\u548c\u672a\u68c0\u67e5\u5f02\u5e38\uff08unchecked exceptions\uff09\u3002\u5df2\u68c0\u67e5\u5f02\u5e38\u5fc5\u987b\u5728\u65b9\u6cd5\u7b7e\u540d\u4e2d\u58f0\u660e\u6216\u5728\u65b9\u6cd5\u5185\u90e8\u5904\u7406\uff0c\u800c\u672a\u68c0\u67e5\u5f02\u5e38\u5219\u4e0d\u9700\u8981\u3002<\/p>\n\n\n\n<p><code>try-catch<\/code>\u5757\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">try {\n    \/\/ \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\n} catch (ExceptionType1 e1) {\n    \/\/ \u5904\u7406\u5f02\u5e38\u7c7b\u578b1\n} catch (ExceptionType2 e2) {\n    \/\/ \u5904\u7406\u5f02\u5e38\u7c7b\u578b2\n} finally {\n    \/\/ \u6700\u7ec8\u6267\u884c\u7684\u4ee3\u7801\n}<\/pre>\n\n\n\n<p><strong>\u7f16\u8bd1\u9636\u6bb5\uff1a\u5b57\u8282\u7801\u751f\u6210<\/strong><\/p>\n\n\n\n<p>\u5f53Java\u6e90\u4ee3\u7801\u88ab\u7f16\u8bd1\u65f6\uff0c<code>try-catch<\/code>\u5757\u88ab\u8f6c\u6362\u4e3a\u5b57\u8282\u7801\u3002\u5728Java\u5b57\u8282\u7801\u4e2d\uff0c\u6bcf\u4e2a<code>try-catch<\/code>\u5757\u5bf9\u5e94\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u8868\uff08exception table\uff09\u6761\u76ee\uff0c\u8fd9\u4e2a\u8868\u7528\u4e8e\u63cf\u8ff0\u5f02\u5e38\u5904\u7406\u7684\u8303\u56f4\u548c\u5904\u7406\u5668\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Java\u793a\u4f8b\u53ca\u5176\u5bf9\u5e94\u7684\u5b57\u8282\u7801\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class ExceptionExample {\n    public void exampleMethod() {\n        try {\n            int a = 1 \/ 0; \/\/ \u53ef\u80fd\u629b\u51faArithmeticException\n        } catch (ArithmeticException e) {\n            System.out.println(\"Caught an exception: \" + e.getMessage());\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u4f7f\u7528<code>javap -c ExceptionExample<\/code>\u53ef\u4ee5\u67e5\u770b\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Compiled from \"ExceptionExample.java\"\npublic class ExceptionExample {\n  public ExceptionExample();\n    Code:\n       0: aload_0\n       1: invokespecial #1                  \/\/ Method java\/lang\/Object.\"&lt;init>\":()V\n       4: return\n\n  public void exampleMethod();\n    Code:\n       0: iconst_1\n       1: iconst_0\n       2: idiv\n       3: istore_1\n       4: goto          12\n       7: astore_1\n       8: getstatic     #2                  \/\/ Field java\/lang\/System.out:Ljava\/io\/PrintStream;\n      11: aload_1\n      12: invokevirtual #3                  \/\/ Method java\/io\/PrintStream.println:(Ljava\/lang\/String;)V\n      15: return\n    Exception table:\n       from    to  target type\n           0     4     7   Class java\/lang\/ArithmeticException\n}<\/pre>\n\n\n\n<p>\u5728\u5b57\u8282\u7801\u4e2d\uff0c<code>Exception table<\/code>\u63cf\u8ff0\u4e86<code>try-catch<\/code>\u5757\u7684\u8303\u56f4\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>from<\/code>\u548c<code>to<\/code>\u6307\u5b9a\u4e86<code>try<\/code>\u5757\u7684\u8303\u56f4\uff0c\u800c<code>target<\/code>\u6307\u5b9a\u4e86\u5f02\u5e38\u5904\u7406\u5668\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<code>type<\/code>\u8868\u793a\u8981\u6355\u83b7\u7684\u5f02\u5e38\u7c7b\u578b\u3002<\/p>\n\n\n\n<p><strong>JVM\u5c42\u9762\u7684\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n\n\n\n<p>\u5728JVM\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u901a\u8fc7\u6808\u5c55\u5f00\uff08stack unwinding\uff09\u673a\u5236\u5b9e\u73b0\u7684\u3002\u5f53\u5f02\u5e38\u629b\u51fa\u65f6\uff0cJVM\u4f1a\u68c0\u67e5\u5f53\u524d\u65b9\u6cd5\u7684\u5f02\u5e38\u5904\u7406\u8868\uff0c\u5bfb\u627e\u5339\u914d\u7684\u5f02\u5e38\u5904\u7406\u5668\u3002\u5982\u679c\u627e\u5230\u5339\u914d\u7684\u5904\u7406\u5668\uff0cJVM\u4f1a\u8df3\u8f6c\u5230\u5f02\u5e38\u5904\u7406\u5668\u7684\u5b57\u8282\u7801\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u5904\u7406\u5668\uff0cJVM\u4f1a\u5c06\u5f02\u5e38\u629b\u5230\u8c03\u7528\u6808\u7684\u4e0a\u4e00\u5c42\uff0c\u7ee7\u7eed\u5bfb\u627e\u5904\u7406\u5668\u3002\u8fd9\u4e00\u8fc7\u7a0b\u4f1a\u4e00\u76f4\u6301\u7eed\u5230\u627e\u5230\u5339\u914d\u7684\u5904\u7406\u5668\u6216\u8005\u6808\u9876\u3002<\/p>\n\n\n\n<p>\u5728JVM\u7684\u5b9e\u73b0\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u8868\u901a\u5e38\u662f\u4ee5\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u5b58\u50a8\uff0c\u4ee5\u4fbf\u5feb\u901f\u67e5\u627e\u3002\u5728HotSpot JVM\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u8868\u662f\u6309\u65b9\u6cd5\u5b58\u50a8\u7684\uff0c\u5e76\u4e14\u5728\u65b9\u6cd5\u8c03\u7528\u65f6\u88ab\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n\n\n\n<p><strong>\u6e90\u7801\u5206\u6790<\/strong><\/p>\n\n\n\n<p>\u8981\u6df1\u5165\u5206\u6790 Java\u5f02\u5e38\u5904\u7406\u7684\u5b9e\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u770b OpenJDK\u7684\u6e90\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u7ec4\u4ef6\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>ClassFileParser<\/strong>\uff1a\u5728\u7c7b\u52a0\u8f7d\u9636\u6bb5\uff0c<code>ClassFileParser<\/code>\u8d1f\u8d23\u89e3\u6790\u7c7b\u6587\u4ef6\uff0c\u5305\u62ec\u5f02\u5e38\u5904\u7406\u8868\u3002<\/li>\n\n\n\n<li><strong>Interpreter<\/strong>\uff1aJVM\u7684\u89e3\u91ca\u5668\u8d1f\u8d23\u6267\u884c\u5b57\u8282\u7801\u6307\u4ee4\uff0c\u5305\u62ec\u5904\u7406\u5f02\u5e38\u3002\u5f53\u9047\u5230<code>athrow<\/code>\u6307\u4ee4\uff08\u7528\u4e8e\u629b\u51fa\u5f02\u5e38\uff09\u65f6\uff0c\u89e3\u91ca\u5668\u4f1a\u542f\u52a8\u5f02\u5e38\u5904\u7406\u6d41\u7a0b\u3002<\/li>\n\n\n\n<li><strong>ExceptionHandling<\/strong>\uff1a\u5728HotSpot JVM\u4e2d\uff0c<code>ExceptionHandling<\/code>\u7c7b\u8d1f\u8d23\u67e5\u627e\u5f02\u5e38\u5904\u7406\u5668\u3002\u5b83\u4f1a\u6839\u636e\u5f53\u524d\u7a0b\u5e8f\u8ba1\u6570\u5668\uff08PC\uff09\u548c\u5f02\u5e38\u7c7b\u578b\uff0c\u5728\u5f02\u5e38\u5904\u7406\u8868\u4e2d\u67e5\u627e\u5339\u914d\u7684\u5904\u7406\u5668\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e9b\u7ec4\u4ef6\u5171\u540c\u534f\u4f5c\uff0c\u5b9e\u73b0\u4e86Java\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002<\/p>\n\n\n\n<p>Java\u4e2d\u7684<code>try-catch<\/code>\u673a\u5236\u901a\u8fc7\u7f16\u8bd1\u5668\u751f\u6210\u7684\u5b57\u8282\u7801\u548cJVM\u7684\u6808\u5c55\u5f00\u673a\u5236\u5b9e\u73b0\uff0c\u5728\u8fd0\u884c\u65f6\uff0cJVM\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u8868\u5feb\u901f\u5b9a\u4f4d\u5f02\u5e38\u5904\u7406\u5668\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u6548\u7684\u5f02\u5e38\u6355\u83b7\u548c\u5904\u7406\u3002\u5c3d\u7ba1\u5f02\u5e38\u5904\u7406\u53ef\u80fd\u5e26\u6765\u4e00\u5b9a\u7684\u6027\u80fd\u5f00\u9500\uff0c\u4f46\u901a\u8fc7\u5408\u7406\u7684\u8bbe\u8ba1\u548c\u4f18\u5316\uff0cJava\u7684\u5f02\u5e38\u673a\u5236\u80fd\u591f\u5728\u4fdd\u8bc1\u7a0b\u5e8f\u5065\u58ee\u6027\u7684\u540c\u65f6\u63d0\u4f9b\u8f83\u597d\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<p><strong><code>try-catch<\/code>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e3a\u4e86\u6700\u5c0f\u5316<code>try-catch<\/code>\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n\n\n\n<p><strong>1. \u4ec5\u5728\u5fc5\u8981\u65f6\u4f7f\u7528\u5f02\u5e38<\/strong><\/p>\n\n\n\n<p>\u5f02\u5e38\u5904\u7406\u5e94\u8be5\u4ec5\u7528\u4e8e\u5904\u7406\u771f\u6b63\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u800c\u4e0d\u662f\u666e\u901a\u7684\u63a7\u5236\u6d41\u3002\u5bf9\u4e8e\u53ef\u4ee5\u9884\u89c1\u7684\u9519\u8bef\u548c\u8fb9\u754c\u60c5\u51b5\uff0c\u4f7f\u7528\u666e\u901a\u7684\u6761\u4ef6\u8bed\u53e5\uff08\u5982<code>if-else<\/code>\uff09\u53ef\u80fd\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n\n\n\n<p><strong>2. \u907f\u514d\u5728\u6027\u80fd\u5173\u952e\u8def\u5f84\u4e2d\u9891\u7e41\u629b\u51fa\u5f02\u5e38<\/strong><\/p>\n\n\n\n<p>\u5728\u6027\u80fd\u5173\u952e\u7684\u4ee3\u7801\u8def\u5f84\u4e2d\uff0c\u5e94\u8be5\u5c3d\u91cf\u907f\u514d\u9891\u7e41\u629b\u51fa\u548c\u6355\u83b7\u5f02\u5e38\u3002\u5982\u679c\u53ef\u80fd\uff0c\u5e94\u8be5\u901a\u8fc7\u9884\u5148\u68c0\u67e5\u6761\u4ef6\u6765\u907f\u514d\u5f02\u5e38\u7684\u53d1\u751f\u3002<\/p>\n\n\n\n<p><strong>3. \u7ec6\u7c92\u5ea6\u7684\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n\n\n\n<p>\u5c06<code>try-catch<\/code>\u5757\u7684\u8303\u56f4\u9650\u5236\u5728\u53ef\u80fd\u53d1\u751f\u5f02\u5e38\u7684\u6700\u5c0f\u4ee3\u7801\u6bb5\u5185\uff0c\u4ee5\u51cf\u5c11\u5f02\u5e38\u5904\u7406\u7684\u8303\u56f4\u548c\u590d\u6742\u6027\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u63d0\u9ad8\u6027\u80fd\uff0c\u8fd8\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u603b\u7684\u6765\u8bf4\uff0c<code>try-catch<\/code>\u7ed3\u6784\u5728\u6ca1\u6709\u5f02\u5e38\u53d1\u751f\u65f6\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u901a\u5e38\u662f\u53ef\u4ee5\u5ffd\u7565\u7684\uff0c\u7136\u800c\uff0c\u4e00\u65e6\u53d1\u751f\u5f02\u5e38\uff0c\u5176\u5f00\u9500\u53ef\u80fd\u4f1a\u663e\u8457\u589e\u52a0\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5f00\u53d1\u4eba\u5458\u9700\u8981\u5728\u6027\u80fd\u548c\u4ee3\u7801\u7684\u5065\u58ee\u6027\u4e4b\u95f4\u8fdb\u884c\u6743\u8861\uff1a\u5f02\u5e38\u5904\u7406\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u4f46\u4e5f\u53ef\u80fd\u5e26\u6765\u6027\u80fd\u5f00\u9500\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u6bd4\u5fae\u5c0f\u7684\u6027\u80fd\u5dee\u5f02\u66f4\u4e3a\u91cd\u8981\uff0c\u7136\u800c\uff0c\u5728\u4e00\u4e9b\u5bf9\u6027\u80fd\u8981\u6c42\u6781\u9ad8\u7684\u573a\u5408\uff0c\u5982\u6e38\u620f\u5f15\u64ce\u3001\u5b9e\u65f6\u7cfb\u7edf\u6216\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u7cfb\u7edf\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u80fd\u9700\u8981\u4ed4\u7ec6\u8bc4\u4f30\u5f02\u5e38\u5904\u7406\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\uff0c\u5e76\u6839\u636e\u5177\u4f53\u60c5\u51b5\u91c7\u53d6\u76f8\u5e94\u7684\u4f18\u5316\u63aa\u65bd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1atry-catch \u4f1a\u5f71\u54cd\u6027\u80fd\u5417\uff1f \u5728\u7f16\u7a0b\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u6355\u83b7\u548c\u5904\u7406\u9519\u8bef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16479"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16479"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16479\/revisions"}],"predecessor-version":[{"id":16480,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16479\/revisions\/16480"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}