{"id":48354,"date":"2024-12-02T17:22:49","date_gmt":"2024-12-02T09:22:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48354\/"},"modified":"2024-12-02T17:22:49","modified_gmt":"2024-12-02T09:22:49","slug":"%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e5%a6%82%e4%bd%95%e5%bd%b1%e5%93%8d-php-%e5%87%bd%e6%95%b0%e7%9a%84%e4%bb%a3%e7%a0%81%e8%a6%86%e7%9b%96%e7%8e%87","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48354\/","title":{"rendered":"\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387\uff0c\u672a\u6355\u83b7\u3001\u90e8\u5206\u6355\u83b7\u6216\u4f7f\u7528\u6cdb\u578b catch \u5757\u6355\u83b7\u5f02\u5e38\u4f1a\u964d\u4f4e\u4ee3\u7801\u8986\u76d6\u7387\u3002\u6b63\u786e\u5904\u7406\u5f02\u5e38\u5bf9\u4e8e\u786e\u4fdd\u4ee3\u7801\u5145\u5206\u8986\u76d6\u81f3\u5173\u91cd\u8981\u30021. \u672a\u6355\u83b7\u5f02\u5e38\u5bfc\u81f4\u672a\u8986\u76d6\u4ee3\u7801\u30022. \u90e8\u5206\u6355\u83b7\u5f02\u5e38\u4f7f catch \u5757\u5916\u4ee3\u7801\u672a\u8986\u76d6\u30023. \u4f7f\u7528\u6cdb\u578b catch \u5757\u6355\u83b7\u6240\u6709\u5f02\u5e38\u5bfc\u81f4 finally \u5757\u5916\u4ee3\u7801\u672a\u8986\u76d6\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730016938671df6aa128c9.jpg\" class=\"aligncenter\" title=\"\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387\u63d2\u56fe\" alt=\"\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u4ee3\u7801\u8986\u76d6\u7387\u8861\u91cf\u6d4b\u8bd5\u6267\u884c\u671f\u95f4\u8986\u76d6\u7684\u4ee3\u7801\u91cf\u3002\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u5f71\u54cd\u4ee3\u7801\u8986\u76d6\u7387\uff0c\u4e86\u89e3\u8fd9\u79cd\u5f71\u54cd\u5bf9\u4e8e\u51c6\u786e\u8bc4\u4f30\u6d4b\u8bd5\u8303\u56f4\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>PHP \u4e2d\u7684\u5f02\u5e38\u5904\u7406\u7531 <code>try-catch-finally<\/code> \u5757\u5904\u7406\u3002<code>try<\/code> \u5757\u5305\u542b\u8981\u6267\u884c\u7684\u4ee3\u7801\uff0c<code>catch<\/code> \u5757\u6355\u83b7\u5e76\u5904\u7406\u9519\u8bef\uff0c\u800c <code>finally<\/code> \u5757\u59cb\u7ec8\u6267\u884c\uff0c\u65e0\u8bba\u662f\u5426\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5bf9\u4ee3\u7801\u8986\u76d6\u7387\u7684\u5f71\u54cd<\/strong><\/p>\n<ul>\n<li><strong>\u672a\u6355\u83b7\u7684\u5f02\u5e38\uff1a<\/strong>\u5982\u679c\u5f02\u5e38\u672a\u88ab\u6355\u83b7\uff0c\u5219\u5728 <code>try<\/code> \u5757\u4e2d\u672a\u6267\u884c\u4e4b\u540e\u7684\u4ee3\u7801\u5c06\u4e0d\u88ab\u89c6\u4e3a\u5df2\u8986\u76d6\u3002<\/li>\n<li><strong>\u90e8\u5206\u6355\u83b7\u7684\u5f02\u5e38\uff1a<\/strong>\u5982\u679c\u5f02\u5e38\u88ab\u6355\u83b7\u4f46\u672a\u5b8c\u5168\u5904\u7406\uff08\u4f8b\u5982\uff0c\u672a\u629b\u51fa\u6216\u91cd\u65b0\u629b\u51fa\uff09\uff0c\u5219 <code>catch<\/code> \u5757\u4e4b\u5916\u7684\u4ee3\u7801\u5c06\u4e0d\u88ab\u89c6\u4e3a\u5df2\u8986\u76d6\u3002<\/li>\n<li><strong>\u6355\u83b7\u6240\u6709\u5f02\u5e38\uff1a<\/strong>\u5982\u679c\u4f7f\u7528\u6cdb\u578b <code>catch<\/code> \u5757\u6355\u83b7\u6240\u6709\u5f02\u5e38\uff0c\u5219 <code>finally<\/code> \u5757\u4e4b\u5916\u7684\u4ee3\u7801\u5c06\u4e0d\u88ab\u89c6\u4e3a\u5df2\u8986\u76d6\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b PHP \u51fd\u6570\uff1a<\/p>\n<pre>function divide(int $a, int $b)\n{\n    try {\n        if ($b === 0) {\n            throw new \\DivisionByZeroError;\n        }\n        \n        return $a \/ $b;\n    } catch (\\DivisionByZeroError $e) {\n        return 0;\n    }\n}<\/pre>\n<p><strong>\u793a\u4f8b\u573a\u666f\uff1a<\/strong><\/p>\n<ol>\n<li><code>$a<\/code> \u4e3a 10\uff0c<code>$b<\/code> \u4e3a 5\uff1a\u65e0\u5f02\u5e38\uff0c\u4ee3\u7801\u8986\u76d6\u7387\u4e3a 100%\u3002<\/li>\n<li><code>$a<\/code> \u4e3a 0\uff0c<code>$b<\/code> \u4e3a 5\uff1a\u5f02\u5e38\u88ab\u6355\u83b7\u4e14\u5904\u7406\uff0c\u4ee3\u7801\u8986\u76d6\u7387\u4e3a 75%\uff08<code>try<\/code> \u5757\u4e2d\u7684 <code>if<\/code> \u8bed\u53e5\u672a\u8986\u76d6\uff09\u3002<\/li>\n<li><code>$a<\/code> \u4e3a 10\uff0c<code>$b<\/code> \u4e3a 0\uff1a\u5f02\u5e38\u672a\u6355\u83b7\uff0c\u4ee3\u7801\u8986\u76d6\u7387\u4e3a 50%\uff08<code>catch<\/code> \u5757\u65e0\u6cd5\u6267\u884c\uff09\u3002<\/li>\n<\/ol>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u6781\u5927\u5730\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387\u3002\u672a\u6355\u83b7\u3001\u90e8\u5206\u6355\u83b7\u6216\u4f7f\u7528\u6cdb\u578b <code>catch<\/code> \u5757\u6355\u83b7\u5f02\u5e38\u90fd\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u8986\u76d6\u7387\u964d\u4f4e\u3002\u5728\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u6b63\u786e\u5904\u7406\u5f02\u5e38\u5bf9\u4e8e\u786e\u4fdd\u4ee3\u7801\u5145\u5206\u8986\u76d6\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u5f02\u5e38\u5904\u7406,\u4ee3\u7801\u8986\u76d6\u7387\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387 \u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60\u6587\u7ae0\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01 \u5f02\u5e38\u5904\u7406\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387\uff0c\u672a\u6355\u83b7\u3001\u90e8\u5206\u6355\u83b7\u6216\u4f7f\u7528\u6cdb\u578b catch \u5757\u6355\u83b7\u5f02\u5e38\u4f1a\u964d\u4f4e\u4ee3\u7801\u8986\u76d6\u7387\u3002\u6b63\u786e\u5904\u7406\u5f02\u5e38\u5bf9\u4e8e\u786e\u4fdd\u4ee3\u7801\u5145\u5206\u8986\u76d6\u81f3\u5173\u91cd\u8981\u30021. \u672a\u6355\u83b7\u5f02\u5e38\u5bfc\u81f4\u672a\u8986\u76d6\u4ee3\u7801\u30022. \u90e8\u5206\u6355\u83b7\u5f02\u5e38\u4f7f catch \u5757\u5916\u4ee3\u7801\u672a\u8986\u76d6\u30023. \u4f7f\u7528\u6cdb\u578b catch \u5757\u6355\u83b7\u6240\u6709\u5f02\u5e38\u5bfc\u81f4 finally \u5757\u5916\u4ee3\u7801\u672a\u8986\u76d6\u3002 \u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd PHP \u51fd\u6570\u7684\u4ee3\u7801\u8986\u76d6\u7387 \u5f15\u8a00 \u4ee3\u7801\u8986\u76d6\u7387\u8861\u91cf\u6d4b\u8bd5\u6267\u884c\u671f\u95f4\u8986\u76d6\u7684\u4ee3\u7801\u91cf\u3002\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u5f71\u54cd\u4ee3\u7801\u8986\u76d6\u7387\uff0c\u4e86\u89e3\u8fd9\u79cd\u5f71\u54cd\u5bf9\u4e8e\u51c6\u786e\u8bc4\u4f30\u6d4b\u8bd5\u8303\u56f4\u81f3\u5173\u91cd\u8981\u3002 \u5f02\u5e38\u5904\u7406 PHP \u4e2d\u7684\u5f02\u5e38\u5904\u7406\u7531 try-catch-finally \u5757\u5904\u7406\u3002try \u5757\u5305\u542b\u8981\u6267\u884c\u7684\u4ee3\u7801\uff0ccatch \u5757\u6355\u83b7\u5e76\u5904\u7406\u9519\u8bef\uff0c\u800c finally \u5757\u59cb\u7ec8\u6267\u884c\uff0c\u65e0\u8bba\u662f\u5426\u629b\u51fa\u5f02\u5e38\u3002 \u5bf9\u4ee3\u7801\u8986\u76d6\u7387\u7684\u5f71\u54cd \u672a\u6355\u83b7\u7684\u5f02\u5e38\uff1a\u5982\u679c\u5f02\u5e38\u672a\u88ab\u6355\u83b7\uff0c\u5219\u5728 try \u5757\u4e2d\u672a\u6267\u884c\u4e4b\u540e\u7684\u4ee3\u7801\u5c06\u4e0d\u88ab\u89c6\u4e3a\u5df2\u8986\u76d6\u3002 \u90e8\u5206\u6355\u83b7\u7684\u5f02\u5e38\uff1a\u5982\u679c\u5f02\u5e38\u88ab\u6355\u83b7\u4f46\u672a\u5b8c\u5168\u5904\u7406\uff08\u4f8b\u5982\uff0c\u672a\u629b\u51fa\u6216\u91cd\u65b0\u629b\u51fa\uff09\uff0c\u5219 catch \u5757\u4e4b\u5916\u7684\u4ee3\u7801\u5c06\u4e0d\u88ab\u89c6\u4e3a\u5df2\u8986\u76d6\u3002 \u6355\u83b7\u6240\u6709\u5f02\u5e38\uff1a\u5982\u679c\u4f7f\u7528\u6cdb\u578b catch \u5757\u6355\u83b7\u6240\u6709\u5f02\u5e38\uff0c\u5219 finally \u5757\u4e4b\u5916\u7684\u4ee3\u7801\u5c06\u4e0d\u88ab\u89c6\u4e3a\u5df2\u8986\u76d6\u3002 \u5b9e\u6218\u6848\u4f8b \u8003\u8651\u4ee5\u4e0b PHP \u51fd\u6570\uff1a function divide(int $a, int $b) [&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-48354","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48354","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=48354"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48354\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}