{"id":48418,"date":"2024-12-02T15:20:21","date_gmt":"2024-12-02T07:20:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48418\/"},"modified":"2024-12-02T15:20:21","modified_gmt":"2024-12-02T07:20:21","slug":"php%e5%87%bd%e6%95%b0%e4%b8%ad%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e7%9a%84%e6%89%a9%e5%b1%95%e6%80%a7%e5%a6%82%e4%bd%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48418\/","title":{"rendered":"PHP\u51fd\u6570\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6269\u5c55\u6027\u5982\u4f55\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u51fd\u6570\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6269\u5c55\u6027\u5982\u4f55\uff1f<\/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\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6269\u5c55\u6027\u5982\u4f55\uff1f\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>\u901a\u8fc7\u521b\u5efa\u548c\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u53ef\u4ee5\u6269\u5c55 PHP \u4e2d\u7684\u5f02\u5e38\u5904\u7406\u529f\u80fd\uff0c\u4ee5\u5c01\u88c5\u7279\u5b9a\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff1a\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a\u901a\u8fc7\u6269\u5c55 Exception \u6216 RuntimeException \u7c7b\u521b\u5efa\u3002\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a\u4f7f\u7528 throw \u5173\u952e\u5b57\u629b\u51fa\u3002\u6355\u6349\u5f02\u5e38\uff1a\u4f7f\u7528 try&#8230;catch \u5757\u6355\u6349\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u793a\u4f8b\uff1aInvalidInputException \u7528\u4e8e\u8868\u793a\u65e0\u6548\u8f93\u5165\uff0c\u901a\u8fc7\u6355\u6349\u6b64\u5f02\u5e38\uff0c\u53ef\u4ee5\u66f4\u5177\u4f53\u5730\u5904\u7406\u9519\u8bef\u60c5\u51b5\uff0c\u5e76\u63d0\u4f9b\u66f4\u52a0\u6709\u610f\u4e49\u7684\u53cd\u9988\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730026363671e1b7b9b49a.jpg\" class=\"aligncenter\" title=\"PHP\u51fd\u6570\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6269\u5c55\u6027\u5982\u4f55\uff1f\u63d2\u56fe\" alt=\"PHP\u51fd\u6570\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6269\u5c55\u6027\u5982\u4f55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6269\u5c55\u6027<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u81ea\u5b9a\u4e49\u5f02\u5e38\u662f\u6269\u5c55\u5f02\u5e38\u5904\u7406\u529f\u80fd\u7684\u6709\u529b\u5de5\u5177\u3002\u901a\u8fc7\u521b\u5efa\u548c\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u5c01\u88c5\u7279\u5b9a\u7684\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u6269\u5c55 <code>Exception<\/code> \u6216 <code>RuntimeException<\/code> \u7c7b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>class MyCustomException extends Exception {}<\/pre>\n<p><strong>\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/p>\n<p>\u8981\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u8bf7\u4f7f\u7528 <code>throw<\/code> \u5173\u952e\u5b57\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>throw new MyCustomException('Custom error message');<\/pre>\n<p><strong>\u6355\u6349\u5f02\u5e38<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>try...catch<\/code> \u5757\u6765\u6355\u6349\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>try {\n  \/\/ \u4ee3\u7801\u5757\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\n} catch (MyCustomException $e) {\n  \/\/ \u5904\u7406 MyCustomException \u5f02\u5e38\n}<\/pre>\n<p><strong>\u6269\u5c55\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u5c55\u793a\u4e86\u81ea\u5b9a\u4e49\u5f02\u5e38\u5982\u4f55\u63d0\u4f9b\u989d\u5916\u7684\u6269\u5c55\u6027\uff1a<\/p>\n<pre>\/\/ \u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u8868\u793a\u65e0\u6548\u8f93\u5165\nclass InvalidInputException extends Exception {}\n\nfunction validateInput($input) {\n  if (!is_string($input)) {\n    throw new InvalidInputException('Input must be a string');\n  }\n}\n\ntry {\n  validateInput(123);\n} catch (InvalidInputException $e) {\n  \/\/ \u81ea\u5b9a\u4e49\u5f02\u5e38\u88ab\u6355\u6349\uff0c\u5e76\u5728\u6b64\u5904\u7406\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>validateInput()<\/code> \u51fd\u6570\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38 <code>InvalidInputException<\/code> \u6765\u8868\u793a\u65e0\u6548\u8f93\u5165\u3002\u901a\u8fc7\u6355\u6349\u6b64\u5f02\u5e38\uff0c\u53ef\u4ee5\u66f4\u5177\u4f53\u5730\u5904\u7406\u9519\u8bef\u60c5\u51b5\uff0c\u5e76\u63d0\u4f9b\u66f4\u52a0\u6709\u610f\u4e49\u7684\u53cd\u9988\u3002<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u5904\u7406\u7279\u5b9a\u9519\u8bef\u60c5\u51b5\uff0c\u6269\u5c55\u5f02\u5e38\u5904\u7406\u529f\u80fd\uff0c\u5e76\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u51fd\u6570\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6269\u5c55\u6027\u5982\u4f55\uff1f \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\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6269\u5c55\u6027\u5982\u4f55\uff1f\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~ \u901a\u8fc7\u521b\u5efa\u548c\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u53ef\u4ee5\u6269\u5c55 PHP \u4e2d\u7684\u5f02\u5e38\u5904\u7406\u529f\u80fd\uff0c\u4ee5\u5c01\u88c5\u7279\u5b9a\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff1a\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a\u901a\u8fc7\u6269\u5c55 Exception \u6216 RuntimeException \u7c7b\u521b\u5efa\u3002\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a\u4f7f\u7528 throw \u5173\u952e\u5b57\u629b\u51fa\u3002\u6355\u6349\u5f02\u5e38\uff1a\u4f7f\u7528 try&#8230;catch \u5757\u6355\u6349\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u793a\u4f8b\uff1aInvalidInputException \u7528\u4e8e\u8868\u793a\u65e0\u6548\u8f93\u5165\uff0c\u901a\u8fc7\u6355\u6349\u6b64\u5f02\u5e38\uff0c\u53ef\u4ee5\u66f4\u5177\u4f53\u5730\u5904\u7406\u9519\u8bef\u60c5\u51b5\uff0c\u5e76\u63d0\u4f9b\u66f4\u52a0\u6709\u610f\u4e49\u7684\u53cd\u9988\u3002 PHP \u51fd\u6570\u4e2d\u5f02\u5e38\u5904\u7406\u7684\u6269\u5c55\u6027 \u5728 PHP \u4e2d\uff0c\u81ea\u5b9a\u4e49\u5f02\u5e38\u662f\u6269\u5c55\u5f02\u5e38\u5904\u7406\u529f\u80fd\u7684\u6709\u529b\u5de5\u5177\u3002\u901a\u8fc7\u521b\u5efa\u548c\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u5c01\u88c5\u7279\u5b9a\u7684\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002 \u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38 \u8981\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u6269\u5c55 Exception \u6216 RuntimeException \u7c7b\u3002\u4f8b\u5982\uff1a class MyCustomException extends Exception {} \u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38 \u8981\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u8bf7\u4f7f\u7528 throw \u5173\u952e\u5b57\uff0c\u5982\u4e0b\u6240\u793a\uff1a throw new MyCustomException(&#8216;Custom error message&#8217;); \u6355\u6349\u5f02\u5e38 \u53ef\u4ee5\u4f7f\u7528 try&#8230;catch \u5757\u6765\u6355\u6349\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u5982\u4e0b\u6240\u793a\uff1a try { \/\/ \u4ee3\u7801\u5757\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38 } catch (MyCustomException $e) { \/\/ \u5904\u7406 MyCustomException [&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-48418","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48418","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=48418"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48418\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}