{"id":47854,"date":"2024-12-02T08:19:02","date_gmt":"2024-12-02T00:19:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47854\/"},"modified":"2024-12-02T08:19:02","modified_gmt":"2024-12-02T00:19:02","slug":"php%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%b8%8e%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e4%b9%8b%e9%97%b4%e7%9a%84%e5%bc%82%e5%90%8c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47854\/","title":{"rendered":"PHP\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u5904\u7406\u4e4b\u95f4\u7684\u5f02\u540c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u5904\u7406\u4e4b\u95f4\u7684\u5f02\u540c<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aPHP\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u5904\u7406\u4e4b\u95f4\u7684\u5f02\u540c\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p>\u5f02\u5e38\u8868\u793a\u610f\u5916\u9519\u8bef\uff0c\u53ef\u901a\u8fc7 try-catch \u5757\u5904\u7406\uff0c\u800c\u9519\u8bef\u8868\u793a\u8f7b\u5fae\u95ee\u9898\uff0c\u7531 PHP \u6838\u5fc3\u89e6\u53d1\u5e76\u901a\u8fc7 error_reporting() \u548c set_error_handler() \u51fd\u6570\u5904\u7406\u3002\u4e8c\u8005\u5728\u89e6\u53d1\u673a\u5236\u3001\u5904\u7406\u65b9\u5f0f\u3001\u53ef\u6062\u590d\u6027\u3001\u8bb0\u5f55\u65b9\u5f0f\u4e0a\u5b58\u5728\u5dee\u5f02\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729940071671cca671178b.jpg\" class=\"aligncenter\" title=\"PHP\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u5904\u7406\u4e4b\u95f4\u7684\u5f02\u540c\u63d2\u56fe\" alt=\"PHP\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u5904\u7406\u4e4b\u95f4\u7684\u5f02\u540c\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u5904\u7406\u4e4b\u95f4\u7684\u5f02\u540c<\/strong><\/p>\n<p>\u5f02\u5e38\u548c\u9519\u8bef\u90fd\u662f PHP \u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u4e8b\u4ef6\uff0c\u4f46\u5b83\u4eec\u5728\u6027\u8d28\u548c\u5904\u7406\u65b9\u5f0f\u4e0a\u5374\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<p><strong>\u5f02\u5e38<\/strong><\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u5f02\u5e38\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u610f\u5916\u6216\u4e0d\u53ef\u6062\u590d\u7684\u60c5\u51b5\u3002\u5b83\u4eec\u7531 <code>try-catch<\/code> \u5757\u5904\u7406\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u5728\u5176\u4e2d\u5b9a\u4e49\u5982\u4f55\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p><strong>\u89e6\u53d1\uff1a<\/strong>\u5f02\u5e38\u53ef\u4ee5\u88ab\u4ee3\u7801\u663e\u5f0f\u629b\u51fa\uff08\u4f7f\u7528 <code>throw<\/code> \u8bed\u53e5\uff09\u6216\u9690\u5f0f\u89e6\u53d1\uff08\u4f8b\u5982\u5f53\u8bbf\u95ee\u672a\u5b9a\u4e49\u7684\u6570\u7ec4\u5143\u7d20\u65f6\uff09\u3002<\/p>\n<p><strong>\u5904\u7406\uff1a<\/strong>\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6355\u83b7\u5f02\u5e38\u3002<code>try<\/code> \u5757\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u800c <code>catch<\/code> \u5757\u5305\u542b\u7528\u4e8e\u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>try {\n    $file = fopen('myfile.txt', 'r');\n    if (!$file) {\n        throw new Exception('\u65e0\u6cd5\u6253\u5f00\u6587\u4ef6');\n    }\n} catch (Exception $e) {\n    echo '\u9519\u8bef\u6d88\u606f\uff1a' . $e-&gt;getMessage();\n}<\/pre>\n<p><strong>\u9519\u8bef<\/strong><\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u9519\u8bef\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u8f7b\u5fae\u6216\u53ef\u6062\u590d\u7684\u95ee\u9898\u3002\u5b83\u4eec\u901a\u5e38\u7531 PHP \u6838\u5fc3\u4ea7\u751f\uff0c\u4e0d\u80fd\u901a\u8fc7 <code>try-catch<\/code> \u5757\u6355\u83b7\u3002<\/p>\n<p><strong>\u89e6\u53d1\uff1a<\/strong>\u9519\u8bef\u7684\u5e38\u89c1\u89e6\u53d1\u539f\u56e0\u5305\u62ec\u8bed\u6cd5\u9519\u8bef\u3001\u81f4\u547d\u9519\u8bef\u3001\u7f16\u8bd1\u65f6\u9519\u8bef\u7b49\u3002<\/p>\n<p><strong>\u5904\u7406\uff1a<\/strong>\u4f7f\u7528 <code>error_reporting()<\/code> \u548c <code>set_error_handler()<\/code> \u51fd\u6570\u914d\u7f6e PHP \u9519\u8bef\u5904\u7406\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<pre>set_error_handler(function($errno, $errstr, $errfile, $errline) {\n    echo \"\u9519\u8bef\u6d88\u606f\uff1a$errstr\uff0c \u9519\u8bef\u884c\u53f7\uff1a$errline\";\n});\n\n$x = 10 \/ 0; \/\/ \u89e6\u53d1\u9664\u96f6\u9519\u8bef<\/pre>\n<p><strong>\u5f02\u540c<\/strong><\/p>\n<ul>\n<li><strong>\u89e6\u53d1\u673a\u5236\uff1a<\/strong>\u5f02\u5e38\u53ef\u4ee5\u88ab\u4ee3\u7801\u663e\u5f0f\u89e6\u53d1\u6216\u9690\u5f0f\u89e6\u53d1\uff0c\u800c\u9519\u8bef\u53ea\u7531 PHP \u6838\u5fc3\u89e6\u53d1\u3002<\/li>\n<li><strong>\u5904\u7406\u65b9\u5f0f\uff1a<\/strong>\u5f02\u5e38\u901a\u8fc7 <code>try-catch<\/code> \u5757\u5904\u7406\uff0c\u800c\u9519\u8bef\u901a\u8fc7 <code>error_reporting()<\/code> \u548c <code>set_error_handler()<\/code> \u51fd\u6570\u5904\u7406\u3002<\/li>\n<li><strong>\u53ef\u6062\u590d\u6027\uff1a<\/strong>\u5f02\u5e38\u901a\u5e38\u8868\u793a\u4e0d\u53ef\u6062\u590d\u7684\u9519\u8bef\uff0c\u800c\u9519\u8bef\u901a\u5e38\u53ef\u4ee5\u6062\u590d\u3002<\/li>\n<li><strong>\u8bb0\u5f55\u65b9\u5f0f\uff1a<\/strong>\u5f02\u5e38\u7684\u8be6\u7ec6\u4fe1\u606f\u53ef\u4ee5\u5b58\u50a8\u5728\u5806\u6808\u8ddf\u8e2a\u4e2d\uff0c\u800c\u9519\u8bef\u7684\u8be6\u7ec6\u4fe1\u606f\u53ea\u80fd\u901a\u8fc7 <code>error_log()<\/code> \u51fd\u6570\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u5904\u7406\u4e4b\u95f4\u7684\u5f02\u540c\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u5904\u7406\u4e4b\u95f4\u7684\u5f02\u540c \u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aPHP\u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u5904\u7406\u4e4b\u95f4\u7684\u5f02\u540c\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01 \u5f02\u5e38\u8868\u793a\u610f\u5916\u9519\u8bef\uff0c\u53ef\u901a\u8fc7 try-catch \u5757\u5904\u7406\uff0c\u800c\u9519\u8bef\u8868\u793a\u8f7b\u5fae\u95ee\u9898\uff0c\u7531 PHP \u6838\u5fc3\u89e6\u53d1\u5e76\u901a\u8fc7 error_reporting() \u548c set_error_handler() \u51fd\u6570\u5904\u7406\u3002\u4e8c\u8005\u5728\u89e6\u53d1\u673a\u5236\u3001\u5904\u7406\u65b9\u5f0f\u3001\u53ef\u6062\u590d\u6027\u3001\u8bb0\u5f55\u65b9\u5f0f\u4e0a\u5b58\u5728\u5dee\u5f02\u3002 PHP \u5f02\u5e38\u5904\u7406\u4e0e\u9519\u8bef\u5904\u7406\u4e4b\u95f4\u7684\u5f02\u540c \u5f02\u5e38\u548c\u9519\u8bef\u90fd\u662f PHP \u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u4e8b\u4ef6\uff0c\u4f46\u5b83\u4eec\u5728\u6027\u8d28\u548c\u5904\u7406\u65b9\u5f0f\u4e0a\u5374\u6709\u6240\u4e0d\u540c\u3002 \u5f02\u5e38 \u5b9a\u4e49\uff1a\u5f02\u5e38\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u610f\u5916\u6216\u4e0d\u53ef\u6062\u590d\u7684\u60c5\u51b5\u3002\u5b83\u4eec\u7531 try-catch \u5757\u5904\u7406\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u5728\u5176\u4e2d\u5b9a\u4e49\u5982\u4f55\u5904\u7406\u5f02\u5e38\u3002 \u89e6\u53d1\uff1a\u5f02\u5e38\u53ef\u4ee5\u88ab\u4ee3\u7801\u663e\u5f0f\u629b\u51fa\uff08\u4f7f\u7528 throw \u8bed\u53e5\uff09\u6216\u9690\u5f0f\u89e6\u53d1\uff08\u4f8b\u5982\u5f53\u8bbf\u95ee\u672a\u5b9a\u4e49\u7684\u6570\u7ec4\u5143\u7d20\u65f6\uff09\u3002 \u5904\u7406\uff1a\u4f7f\u7528 try-catch \u5757\u6355\u83b7\u5f02\u5e38\u3002try \u5757\u5305\u542b\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u800c catch \u5757\u5305\u542b\u7528\u4e8e\u5904\u7406\u5f02\u5e38\u7684\u4ee3\u7801\u3002 \u5b9e\u6218\u6848\u4f8b\uff1a try { $file = fopen(&#8216;myfile.txt&#8217;, &#8216;r&#8217;); if (!$file) { throw new Exception(&#8216;\u65e0\u6cd5\u6253\u5f00\u6587\u4ef6&#8217;); } } catch (Exception $e) { echo &#8216;\u9519\u8bef\u6d88\u606f\uff1a&#8217; . $e-&gt;getMessage(); } \u9519\u8bef \u5b9a\u4e49\uff1a\u9519\u8bef\u8868\u793a\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u8f7b\u5fae\u6216\u53ef\u6062\u590d\u7684\u95ee\u9898\u3002\u5b83\u4eec\u901a\u5e38\u7531 [&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-47854","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47854","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=47854"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47854\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}