{"id":46966,"date":"2024-12-02T09:46:45","date_gmt":"2024-12-02T01:46:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46966\/"},"modified":"2024-12-02T09:46:45","modified_gmt":"2024-12-02T01:46:45","slug":"php-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e5%92%8c%e4%bd%bf%e7%94%a8%e8%87%aa%e5%ae%9a%e4%b9%89%e5%bc%82%e5%b8%b8%e7%b1%bb%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46966\/","title":{"rendered":"PHP \u5f02\u5e38\u5904\u7406\u4e2d\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u5f02\u5e38\u5904\u7406\u4e2d\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff1f<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPHP \u5f02\u5e38\u5904\u7406\u4e2d\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p>PHP \u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u53ef\u7528\u4e8e\u5904\u7406\u7279\u5b9a\u9700\u6c42\u7684\u9519\u8bef\u60c5\u51b5\u3002\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u65f6\uff0c\u9700\u8981\u7ee7\u627f\u81ea Exception \u6216 Error \u7c7b\uff0c\u5e76\u5b9a\u4e49\u6784\u9020\u51fd\u6570\u3002\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u65f6\uff0c\u9700\u5148 include \u5f02\u5e38\u7c7b\u6587\u4ef6\uff0c\u518d\u4f7f\u7528 throw \u8bed\u53e5\u629b\u51fa\u5f02\u5e38\u3002\u5b9e\u6218\u4e2d\uff0c\u53ef\u901a\u8fc7\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u975e\u6570\u5b57\u53c2\u6570\u4f20\u5165\u51fd\u6570\u7b49\u9519\u8bef\u60c5\u51b5\uff0c\u5e76\u5728 try-catch \u5757\u4e2d\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729857638671b886635897.jpg\" class=\"aligncenter\" title=\"PHP \u5f02\u5e38\u5904\u7406\u4e2d\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff1f\u63d2\u56fe\" alt=\"PHP \u5f02\u5e38\u5904\u7406\u4e2d\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u5f02\u5e38\u5904\u7406\uff1a\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u5fc5\u4e0d\u53ef\u5c11\u7684\u4e00\u90e8\u5206\u3002\u5b83\u4f7f\u6211\u4eec\u80fd\u591f\u4ee5\u53d7\u63a7\u548c\u6709\u7528\u7684\u65b9\u5f0f\u5904\u7406\u9519\u8bef\u60c5\u51b5\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u5957\u5185\u7f6e\u7684\u5f02\u5e38\u7c7b\uff0c\u4f46\u6211\u4eec\u6709\u65f6\u9700\u8981\u521b\u5efa\u81ea\u5df1\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u6765\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<\/strong><\/p>\n<p>\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u7684\u683c\u5f0f\u4e0e\u5185\u7f6e\u5f02\u5e38\u7c7b\u7c7b\u4f3c\u3002\u5b83\u4eec\u5fc5\u987b\u7ee7\u627f\u81ea <code>Exception<\/code> \u6216 <code>Error<\/code> \u7c7b\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>class MyCustomException extends Exception\n{\n    public function __construct($message, $code = 0, Throwable $previous = null)\n    {\n        \/\/ \u8c03\u7528 Exception \u6784\u9020\u51fd\u6570\n        parent::__construct($message, $code, $previous);\n    }\n}<\/pre>\n<p><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<\/strong><\/p>\n<p>\u5728\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u5305\u62ec\u5305\u542b\u8be5\u5f02\u5e38\u7c7b\u7684\u6587\u4ef6\u3002<\/p>\n<pre>&lt;?php\nrequire_once 'MyCustomException.php';<\/pre>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>throw<\/code> \u8bed\u53e5\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a<\/p>\n<pre>throw new MyCustomException('\u81ea\u5b9a\u4e49\u5f02\u5e38\u6d88\u606f');<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u4e24\u6570\u4e4b\u548c\uff0c\u4f46\u662f\u5f53\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u4e0d\u662f\u6570\u5b57\u65f6\u4f1a\u51fa\u73b0\u9519\u8bef\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u6765\u5904\u7406\u8fd9\u79cd\u9519\u8bef\uff1a<\/p>\n<pre>function add($num1, $num2)\n{\n    if (!is_numeric($num1) || !is_numeric($num2)) {\n        throw new MyCustomException('\u8f93\u5165\u5fc5\u987b\u662f\u6570\u5b57');\n    }\n\n    return $num1 + $num2;\n}<\/pre>\n<p>\u5728\u4f7f\u7528\u6b64\u51fd\u6570\u65f6\uff0c\u5982\u679c\u4f20\u9012\u4e86\u975e\u6570\u5b57\u53c2\u6570\uff0c\u5b83\u5c06\u629b\u51fa <code>MyCustomException<\/code> \u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u6b64\u5f02\u5e38\uff1a<\/p>\n<pre>try {\n    $result = add(5, 'a');\n} catch (MyCustomException $e) {\n    echo $e-&gt;getMessage(); \/\/ \u8f93\u51fa\uff1a\u8f93\u5165\u5fc5\u987b\u662f\u6570\u5b57\n}<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP \u5f02\u5e38\u5904\u7406\u4e2d\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff1f\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\u4e2d\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff1f \u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPHP \u5f02\u5e38\u5904\u7406\u4e2d\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01 PHP \u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u53ef\u7528\u4e8e\u5904\u7406\u7279\u5b9a\u9700\u6c42\u7684\u9519\u8bef\u60c5\u51b5\u3002\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u65f6\uff0c\u9700\u8981\u7ee7\u627f\u81ea Exception \u6216 Error \u7c7b\uff0c\u5e76\u5b9a\u4e49\u6784\u9020\u51fd\u6570\u3002\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u65f6\uff0c\u9700\u5148 include \u5f02\u5e38\u7c7b\u6587\u4ef6\uff0c\u518d\u4f7f\u7528 throw \u8bed\u53e5\u629b\u51fa\u5f02\u5e38\u3002\u5b9e\u6218\u4e2d\uff0c\u53ef\u901a\u8fc7\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u975e\u6570\u5b57\u53c2\u6570\u4f20\u5165\u51fd\u6570\u7b49\u9519\u8bef\u60c5\u51b5\uff0c\u5e76\u5728 try-catch \u5757\u4e2d\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002 PHP \u5f02\u5e38\u5904\u7406\uff1a\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b \u7b80\u4ecb \u5f02\u5e38\u5904\u7406\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u5fc5\u4e0d\u53ef\u5c11\u7684\u4e00\u90e8\u5206\u3002\u5b83\u4f7f\u6211\u4eec\u80fd\u591f\u4ee5\u53d7\u63a7\u548c\u6709\u7528\u7684\u65b9\u5f0f\u5904\u7406\u9519\u8bef\u60c5\u51b5\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u5957\u5185\u7f6e\u7684\u5f02\u5e38\u7c7b\uff0c\u4f46\u6211\u4eec\u6709\u65f6\u9700\u8981\u521b\u5efa\u81ea\u5df1\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u6765\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002 \u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b \u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u7684\u683c\u5f0f\u4e0e\u5185\u7f6e\u5f02\u5e38\u7c7b\u7c7b\u4f3c\u3002\u5b83\u4eec\u5fc5\u987b\u7ee7\u627f\u81ea Exception \u6216 Error \u7c7b\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u7684\u793a\u4f8b\uff1a class MyCustomException extends Exception { public function __construct($message, $code = 0, Throwable $previous = null) { \/\/ \u8c03\u7528 Exception \u6784\u9020\u51fd\u6570 parent::__construct($message, $code, $previous); } } \u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b \u5728\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u5305\u62ec\u5305\u542b\u8be5\u5f02\u5e38\u7c7b\u7684\u6587\u4ef6\u3002 &lt;?php [&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-46966","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46966","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=46966"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46966\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}