{"id":46941,"date":"2024-12-02T09:53:07","date_gmt":"2024-12-02T01:53:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46941\/"},"modified":"2024-12-02T09:53:07","modified_gmt":"2024-12-02T01:53:07","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e6%8a%80%e5%b7%a7%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46941\/","title":{"rendered":"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6280\u5de7\u8be6\u89e3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6280\u5de7\u8be6\u89e3<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6280\u5de7\u8be6\u89e3\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>PHP \u5f02\u5e38\u5904\u7406\u901a\u8fc7 try\u3001catch\u3001finally \u8bed\u53e5\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c preventing \u7a0b\u5e8f\u5d29\u6e83\u3002\u5e38\u89c1\u5f02\u5e38\u7c7b\u578b\u5305\u62ec\uff1a\u6570\u5b66\u64cd\u4f5c\u9519\u8bef (ArithmeticError)\u65ad\u8a00\u5931\u8d25\u9519\u8bef (AssertionError)\u9664\u4ee5\u96f6\u9519\u8bef (DivisionByZeroError)\u8bed\u6cd5\u9519\u8bef (ParseError)\u7c7b\u578b\u9519\u8bef (TypeError)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729855908671b81a427684.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6280\u5de7\u8be6\u89e3\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6280\u5de7\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6280\u5de7\u8be6\u89e3<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u5141\u8bb8\u7a0b\u5e8f\u5728\u53d1\u751f\u5f02\u5e38\u60c5\u51b5\u65f6\u4f18\u96c5\u5730\u5931\u8d25\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002\u5728 PHP \u4e2d\uff0c\u5f02\u5e38\u901a\u8fc7 <code>try<\/code>\u3001<code>catch<\/code> \u548c <code>finally<\/code> \u8bed\u53e5\u6765\u5904\u7406\u3002<\/p>\n<p><strong>try-catch-finally<\/strong><\/p>\n<p><code>try<\/code> \u5757\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>try {\n    \/\/ \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n}<\/pre>\n<p><code>catch<\/code> \u5757\u7528\u4e8e\u6355\u83b7\u5f02\u5e38\u5e76\u6267\u884c\u6062\u590d\u64cd\u4f5c\uff1a<\/p>\n<pre>catch (Throwable $e) {\n    \/\/ \u5f02\u5e38\u5904\u7406\u4ee3\u7801\n}<\/pre>\n<p><code>finally<\/code> \u5757\u7528\u4e8e\u5728\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>finally {\n    \/\/ \u6e05\u7406\u4ee3\u7801\n}<\/pre>\n<p><strong>\u5f02\u5e38\u7c7b\u578b<\/strong><\/p>\n<p>PHP \u4e2d\u5e38\u89c1\u7684\u5f02\u5e38\u7c7b\u578b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>ArithmeticError<\/code>\uff1a\u6570\u5b66\u64cd\u4f5c\u9519\u8bef<\/li>\n<li><code>AssertionError<\/code>\uff1a\u65ad\u8a00\u5931\u8d25\u9519\u8bef<\/li>\n<li><code>DivisionByZeroError<\/code>\uff1a\u9664\u4ee5\u96f6\u9519\u8bef<\/li>\n<li><code>ParseError<\/code>\uff1a\u4ee3\u7801\u8bed\u6cd5\u9519\u8bef<\/li>\n<li><code>TypeError<\/code>\uff1a\u7c7b\u578b\u9519\u8bef<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u5904\u7406\u6587\u4ef6\u6253\u5f00\u5931\u8d25\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>try {\n    $file = fopen('file_not_exist.txt', 'r');\n} catch (FileNotFoundException $e) {\n    \/\/ \u6587\u4ef6\u4e0d\u5b58\u5728\u5f02\u5e38\u5904\u7406\n} finally {\n    \/\/ \u5173\u95ed\u6587\u4ef6\u6d41\u7684\u6e05\u7406\u4ee3\u7801\uff08\u5982\u679c\u6253\u5f00\u4e86\u6587\u4ef6\uff09\n}<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5f53\u6587\u4ef6\u4e0d\u5b58\u5728\u65f6\uff0c<code>catch<\/code> \u5757\u5c06\u6355\u83b7 <code>FileNotFoundException<\/code> \u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u4f8b\u5982\u663e\u793a\u9519\u8bef\u6d88\u606f\u6216\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f <code>Throwable<\/code> \u7c7b\u6765\u521b\u5efa\u81ea\u5df1\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a<\/p>\n<pre>class MyCustomException extends Throwable {\n    \/\/ ...\n}<\/pre>\n<p>\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u6765\u5904\u7406\u7279\u5b9a\u7684\u9519\u8bef\u6761\u4ef6\uff1a<\/p>\n<pre>throw new MyCustomException('\u9519\u8bef\u4fe1\u606f');<\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u5904\u7406\u610f\u5916\u9519\u8bef\uff0c\u800c\u4e0d\u662f\u9884\u671f\u9519\u8bef\uff08\u4f8b\u5982\u8f93\u5165\u9a8c\u8bc1\uff09\u3002<\/li>\n<li>\u4ec5\u629b\u51fa\u771f\u6b63\u4e0d\u53ef\u6062\u590d\u7684\u9519\u8bef\u3002<\/li>\n<li>\u7f16\u5199\u5f02\u5e38\u6d88\u606f\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u53ef\u8c03\u8bd5\u6027\u3002<\/li>\n<li>\u5728 <code>finally<\/code> \u5757\u4e2d\u6267\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u8d44\u6e90\u59cb\u7ec8\u6b63\u786e\u91ca\u653e\u3002<\/li>\n<\/ul>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6280\u5de7\u8be6\u89e3\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6280\u5de7\u8be6\u89e3 \u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6280\u5de7\u8be6\u89e3\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~ PHP \u5f02\u5e38\u5904\u7406\u901a\u8fc7 try\u3001catch\u3001finally \u8bed\u53e5\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c preventing \u7a0b\u5e8f\u5d29\u6e83\u3002\u5e38\u89c1\u5f02\u5e38\u7c7b\u578b\u5305\u62ec\uff1a\u6570\u5b66\u64cd\u4f5c\u9519\u8bef (ArithmeticError)\u65ad\u8a00\u5931\u8d25\u9519\u8bef (AssertionError)\u9664\u4ee5\u96f6\u9519\u8bef (DivisionByZeroError)\u8bed\u6cd5\u9519\u8bef (ParseError)\u7c7b\u578b\u9519\u8bef (TypeError) PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6280\u5de7\u8be6\u89e3 \u5f02\u5e38\u5904\u7406\u5141\u8bb8\u7a0b\u5e8f\u5728\u53d1\u751f\u5f02\u5e38\u60c5\u51b5\u65f6\u4f18\u96c5\u5730\u5931\u8d25\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002\u5728 PHP \u4e2d\uff0c\u5f02\u5e38\u901a\u8fc7 try\u3001catch \u548c finally \u8bed\u53e5\u6765\u5904\u7406\u3002 try-catch-finally try \u5757\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff1a try { \/\/ \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801 } catch \u5757\u7528\u4e8e\u6355\u83b7\u5f02\u5e38\u5e76\u6267\u884c\u6062\u590d\u64cd\u4f5c\uff1a catch (Throwable $e) { \/\/ \u5f02\u5e38\u5904\u7406\u4ee3\u7801 } finally \u5757\u7528\u4e8e\u5728\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u7684\u4ee3\u7801\uff1a finally { \/\/ \u6e05\u7406\u4ee3\u7801 } \u5f02\u5e38\u7c7b\u578b PHP \u4e2d\u5e38\u89c1\u7684\u5f02\u5e38\u7c7b\u578b\u5305\u62ec\uff1a ArithmeticError\uff1a\u6570\u5b66\u64cd\u4f5c\u9519\u8bef AssertionError\uff1a\u65ad\u8a00\u5931\u8d25\u9519\u8bef DivisionByZeroError\uff1a\u9664\u4ee5\u96f6\u9519\u8bef ParseError\uff1a\u4ee3\u7801\u8bed\u6cd5\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":[16],"tags":[],"class_list":["post-46941","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=46941"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46941\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}