{"id":46954,"date":"2024-12-02T13:15:49","date_gmt":"2024-12-02T05:15:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46954\/"},"modified":"2024-12-02T13:15:49","modified_gmt":"2024-12-02T05:15:49","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46954\/","title":{"rendered":"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5<\/h1>\n<p>\u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729857093671b864572bf5.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u7528\u4e8e\u5904\u7406\u610f\u5916\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u7684\u673a\u5236\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 try-catch \u5757\u4ee5\u53ca set_exception_handler() \u51fd\u6570\u6765\u5b9e\u73b0\u5f02\u5e38\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u5e2e\u52a9\u60a8\u5728 PHP \u4ee3\u7801\u4e2d\u6709\u6548\u5730\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p><strong>\u4f7f\u7528 try-catch \u5757<\/strong><\/p>\n<p>try-catch \u5757\u662f\u6700\u5e38\u89c1\u7684\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u3002\u5b83\u5305\u542b\u4e00\u4e2a try \u5757\uff0c\u5728\u6b64\u5757\u4e2d\u6267\u884c\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u4ee5\u53ca\u4e00\u4e2a catch \u5757\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u5df2\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/p>\n<pre>try {\n  \/\/ \u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\n} catch (Exception $e) {\n  \/\/ \u5f02\u5e38\u5904\u7406\u4ee3\u7801\n}<\/pre>\n<p><strong>\u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f<\/strong><\/p>\n<p>PHP \u4e5f\u5141\u8bb8\u60a8\u8bbe\u7f6e\u4e00\u4e2a\u5168\u5c40\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\uff0c\u5b83\u5c06\u5904\u7406\u7531\u672a\u6355\u83b7\u7684\u5f02\u5e38\u629b\u51fa\u7684\u6240\u6709\u5f02\u5e38\u3002<\/p>\n<pre>set_exception_handler(function (Exception $e) {\n  \/\/ \u5728\u8fd9\u91cc\u5904\u7406\u5f02\u5e38\n});<\/pre>\n<p><strong>\u4f20\u9012\u548c\u91cd\u65b0\u629b\u51fa\u5f02\u5e38<\/strong><\/p>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5c06\u5f02\u5e38\u4f20\u9012\u7ed9\u8c03\u7528\u51fd\u6570\u6216\u4f7f\u7528 rethrow \u8bed\u53e5\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<pre>function myFunction() {\n  try {\n    \/\/ \u4ee3\u7801\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\n  } catch (Exception $e) {\n    throw $e;  \/\/ \u91cd\u65b0\u629b\u51fa\u5f02\u5e38\n  }\n}<\/pre>\n<p><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<\/strong><\/p>\n<p>PHP \u5185\u7f6e\u7684 Exception \u7c7b\u53ef\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u9700\u6c42\u3002\u4f46\u662f\uff0c\u60a8\u8fd8\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff0c\u4ee5\u63d0\u4f9b\u66f4\u5177\u4f53\u7684\u9519\u8bef\u6d88\u606f\u548c\u5904\u7406\u903b\u8f91\u3002<\/p>\n<pre>class MyCustomException extends Exception {\n  \/\/ ...\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5728\u5904\u7406\u6570\u636e\u5e93\u67e5\u8be2\u65f6\u7684\u5f02\u5e38\u5904\u7406\u793a\u4f8b\uff1a<\/p>\n<pre>try {\n  $conn = new PDO('mysql:host=localhost;dbname=test', 'root', '');\n  $stmt = $conn-&gt;prepare('SELECT * FROM users WHERE id = ?');\n  $stmt-&gt;execute(array(1));\n} catch (PDOException $e) {\n  \/\/ \u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u6216\u67e5\u8be2\u9519\u8bef\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\u5c06\u4f7f\u60a8\u80fd\u591f\u6709\u6548\u5730\u5904\u7406 PHP \u4ee3\u7801\u4e2d\u7684\u5f02\u5e38\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u8fd9\u5c06\u6709\u52a9\u4e8e\u60a8\u521b\u5efa\u5065\u58ee\u4e14\u53ef\u9760\u7684\u8f6f\u4ef6\u7cfb\u7edf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,\u5f02\u5e38\u5904\u7406\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5 \u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60\u6587\u7ae0\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~ PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6700\u4f73\u5b9e\u8df5 \u7b80\u4ecb \u5f02\u5e38\u5904\u7406\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u7528\u4e8e\u5904\u7406\u610f\u5916\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u7684\u673a\u5236\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 try-catch \u5757\u4ee5\u53ca set_exception_handler() \u51fd\u6570\u6765\u5b9e\u73b0\u5f02\u5e38\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u5e2e\u52a9\u60a8\u5728 PHP \u4ee3\u7801\u4e2d\u6709\u6548\u5730\u5904\u7406\u5f02\u5e38\u3002 \u4f7f\u7528 try-catch \u5757 try-catch \u5757\u662f\u6700\u5e38\u89c1\u7684\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u3002\u5b83\u5305\u542b\u4e00\u4e2a try \u5757\uff0c\u5728\u6b64\u5757\u4e2d\u6267\u884c\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u4ee5\u53ca\u4e00\u4e2a catch \u5757\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u5df2\u629b\u51fa\u7684\u5f02\u5e38\u3002 try { \/\/ \u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801 } catch (Exception $e) { \/\/ \u5f02\u5e38\u5904\u7406\u4ee3\u7801 } \u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f PHP \u4e5f\u5141\u8bb8\u60a8\u8bbe\u7f6e\u4e00\u4e2a\u5168\u5c40\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\uff0c\u5b83\u5c06\u5904\u7406\u7531\u672a\u6355\u83b7\u7684\u5f02\u5e38\u629b\u51fa\u7684\u6240\u6709\u5f02\u5e38\u3002 set_exception_handler(function (Exception $e) { \/\/ \u5728\u8fd9\u91cc\u5904\u7406\u5f02\u5e38 }); \u4f20\u9012\u548c\u91cd\u65b0\u629b\u51fa\u5f02\u5e38 \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5c06\u5f02\u5e38\u4f20\u9012\u7ed9\u8c03\u7528\u51fd\u6570\u6216\u4f7f\u7528 rethrow \u8bed\u53e5\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\u3002 function myFunction() { try [&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-46954","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46954","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=46954"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46954\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}