{"id":46929,"date":"2024-12-02T08:04:51","date_gmt":"2024-12-02T00:04:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46929\/"},"modified":"2024-12-02T08:04:51","modified_gmt":"2024-12-02T00:04:51","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-php-%e5%87%bd%e6%95%b0%e5%a4%84%e7%90%86%e5%a4%96%e9%83%a8%e5%87%bd%e6%95%b0%e4%b8%ad%e5%bc%95%e5%8f%91%e7%9a%84%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46929\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p>\u4f7f\u7528 PHP \u51fd\u6570 set_exception_handler() \u53ef\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38\uff1a\u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u51fd\u6570\uff0c\u5b83\u5c06\u6355\u83b7\u672a\u629b\u51fa\u6216\u5904\u7406\u7684\u81f4\u547d\u9519\u8bef\/\u5f02\u5e38\u3002\u5f02\u5e38\u5904\u7406\u51fd\u6570\u63a5\u6536 Exception \u5bf9\u8c61\uff0c\u5305\u542b\u5f02\u5e38\u4fe1\u606f\u3002\u901a\u8fc7 try&#8230;catch \u5757\u53ef\u624b\u52a8\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729854945671b7de1c39e2.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38<\/strong><\/p>\n<p>PHP \u51fd\u6570 <code>set_exception_handler()<\/code> \u53ef\u4ee5\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38\u3002\u8bbe\u7f6e\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u51fd\u6570\u540e\uff0c\u5b83\u5c06\u6355\u83b7\u4efb\u4f55\u6ca1\u6709\u629b\u51fa\u6216\u5904\u7406\u7684\u81f4\u547d\u9519\u8bef\u6216\u5f02\u5e38\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>function exception_handler($exception) {\n    \/\/ \u5904\u7406\u5f02\u5e38\n}\n\nset_exception_handler('exception_handler');<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>calculate()<\/code> \u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u5bfc\u81f4\u9664\u4ee5\u96f6\u7684\u9519\u8bef\uff1a<\/p>\n<pre>function calculate($dividend, $divisor) {\n    if ($divisor == 0) {\n        throw new Exception('Division by zero');\n    }\n\n    return $dividend \/ $divisor;\n}<\/pre>\n<p>\u8981\u5728\u8c03\u7528 <code>calculate()<\/code> \u65f6\u5904\u7406\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>set_exception_handler(function ($exception) {\n    echo 'Error: ' . $exception-&gt;getMessage();\n});\n\ntry {\n    $result = calculate(10, 0);\n    echo 'Result: ' . $result;\n} catch (Exception $e) {\n    echo 'Caught exception: ' . $e-&gt;getMessage();\n}<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>Error: Division by zero<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u51fd\u6570\u63a5\u6536\u4e00\u4e2a <code>Exception<\/code> \u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5b83\u5305\u542b\u5f02\u5e38\u7684\u4fe1\u606f\u3002<\/li>\n<li>\u901a\u8fc7 <code>try...catch<\/code> \u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u51fd\u6570\uff0c\u5219 PHP \u4f1a\u8f93\u51fa\u4e00\u4e2a\u5e26\u6709\u9519\u8bef\u4fe1\u606f\u7684\u6d4f\u89c8\u5668\u9519\u8bef\u9875\u9762\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38 \u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002 \u4f7f\u7528 PHP \u51fd\u6570 set_exception_handler() \u53ef\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38\uff1a\u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u51fd\u6570\uff0c\u5b83\u5c06\u6355\u83b7\u672a\u629b\u51fa\u6216\u5904\u7406\u7684\u81f4\u547d\u9519\u8bef\/\u5f02\u5e38\u3002\u5f02\u5e38\u5904\u7406\u51fd\u6570\u63a5\u6536 Exception \u5bf9\u8c61\uff0c\u5305\u542b\u5f02\u5e38\u4fe1\u606f\u3002\u901a\u8fc7 try&#8230;catch \u5757\u53ef\u624b\u52a8\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002 \u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38 PHP \u51fd\u6570 set_exception_handler() \u53ef\u4ee5\u5904\u7406\u5916\u90e8\u51fd\u6570\u4e2d\u5f15\u53d1\u7684\u5f02\u5e38\u3002\u8bbe\u7f6e\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u51fd\u6570\u540e\uff0c\u5b83\u5c06\u6355\u83b7\u4efb\u4f55\u6ca1\u6709\u629b\u51fa\u6216\u5904\u7406\u7684\u81f4\u547d\u9519\u8bef\u6216\u5f02\u5e38\u3002 \u4f7f\u7528\u65b9\u6cd5\uff1a function exception_handler($exception) { \/\/ \u5904\u7406\u5f02\u5e38 } set_exception_handler(&#8216;exception_handler&#8217;); \u5b9e\u6218\u6848\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a calculate() \u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u5bfc\u81f4\u9664\u4ee5\u96f6\u7684\u9519\u8bef\uff1a function calculate($dividend, $divisor) { if ($divisor == 0) { throw new Exception(&#8216;Division by zero&#8217;); } return $dividend \/ $divisor; } \u8981\u5728\u8c03\u7528 [&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-46929","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46929","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=46929"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46929\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}