{"id":47166,"date":"2024-12-02T14:16:38","date_gmt":"2024-12-02T06:16:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47166\/"},"modified":"2024-12-02T14:16:38","modified_gmt":"2024-12-02T06:16:38","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%ef%bc%9a%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98%e5%8f%8a%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47166\/","title":{"rendered":"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\uff1a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\uff1a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\uff1a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p>PHP \u5f02\u5e38\u5904\u7406\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u901a\u8fc7 try\/catch\/finally \u6355\u83b7\u9519\u8bef\uff0c\u5e76\u53ef\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff08\u7ee7\u627f\u81ea Exception \u7c7b\uff09\u3002\u5e38\u89c1\u95ee\u9898\u5305\u62ec\uff1aPHP \u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\uff08Exception\u3001Error\u3001LogicException\u3001RuntimeException\uff09\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5f02\u5e38\u7c7b\u578b\u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\u7b49\u60c5\u51b5\uff0c\u540c\u65f6\u63d0\u4f9b\u5efa\u8bae\u4ee5\u4f7f\u7528\u7279\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b\u3001\u53d8\u91cf\u5316\u5f02\u5e38\u6d88\u606f\u548c\u8c28\u614e\u4f7f\u7528 finally \u8bed\u53e5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729902343671c37075b393.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\uff1a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\uff1a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\uff1a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u7684\u4e00\u79cd\u673a\u5236\u3002\u5b83\u5141\u8bb8\u60a8\u4ee5\u53d7\u63a7\u548c\u4e00\u81f4\u7684\u65b9\u5f0f\u54cd\u5e94\u610f\u5916\u60c5\u51b5\u3002\u901a\u8fc7\u5904\u7406\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u63d0\u4f9b\u6709\u7528\u7684\u53cd\u9988\u3001\u8bb0\u5f55\u9519\u8bef\u8be6\u7ec6\u4fe1\u606f\uff0c\u5e76\u9632\u6b62\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<p><strong>\u5e38\u89c1\u95ee\u9898<\/strong><\/p>\n<ul>\n<li><strong>PHP \u629b\u51fa\u4ec0\u4e48\u7c7b\u578b\u7684\u5f02\u5e38\uff1f<\/strong><\/li>\n<\/ul>\n<p>PHP \u629b\u51fa\u5404\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>Exception<\/code>\uff1a\u4e00\u822c\u5f02\u5e38\u7c7b<\/li>\n<li><code>Error<\/code>\uff1a\u4e0d\u53ef\u6062\u590d\u7684\u5f02\u5e38<\/li>\n<li><code>LogicException<\/code>\uff1a\u903b\u8f91\u9519\u8bef<\/li>\n<li><code>RuntimeException<\/code>\uff1a\u8fd0\u884c\u65f6\u9519\u8bef<\/li>\n<li><strong>\u5982\u4f55\u6355\u83b7\u5f02\u5e38\uff1f<\/strong><\/li>\n<\/ul>\n<p>\u4f7f\u7528 <code>try<\/code>\u3001<code>catch<\/code> \u548c <code>finally<\/code> \u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff1a<\/p>\n<pre>try {\n  \/\/ \u4ee3\u7801\u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\n} catch (Exception $e) {\n  \/\/ \u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\n} finally {\n  \/\/ \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u59cb\u7ec8\u6267\u884c\u7684\u4ee3\u7801\n}<\/pre>\n<ul>\n<li><strong>\u5982\u4f55\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1f<\/strong><\/li>\n<\/ul>\n<p>\u8981\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u8bf7\u6269\u5c55 <code>Exception<\/code> \u7c7b\uff1a<\/p>\n<pre>class MyCustomException extends Exception {\n  \/\/ ...\n}\n\n\/\/ \u629b\u51fa\u5f02\u5e38\nthrow new MyCustomException(\"\u81ea\u5b9a\u4e49\u5f02\u5e38\u6d88\u606f\");<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u6267\u884c\u67e5\u8be2\u7684\u51fd\u6570\uff1a<\/p>\n<pre>function connect_db() {\n  $mysqli = new mysqli('localhost', 'root', 'password', 'database');\n\n  if ($mysqli-&gt;connect_errno) {\n    throw new RuntimeException(\"\u65e0\u6cd5\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff1a\" . $mysqli-&gt;connect_error);\n  }\n\n  return $mysqli;\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u629b\u51fa <code>RuntimeException<\/code> \u5f02\u5e38\uff0c\u4ee5\u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\u7684\u60c5\u51b5\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u8bb0\u5f55\u9519\u8bef\u6d88\u606f\u5e76\u505a\u51fa\u9002\u5f53\u7684\u54cd\u5e94\uff0c\u4f8b\u5982\u663e\u793a\u9519\u8bef\u6d88\u606f\u7ed9\u7528\u6237\u3002<\/p>\n<p><strong>\u5176\u4ed6\u63d0\u793a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 <code>printf<\/code> \u6216 <code>sprintf<\/code> \u51fd\u6570\u5c06\u5f02\u5e38\u6d88\u606f\u53d8\u91cf\u5316\u3002<\/li>\n<li>\u8003\u8651\u4f7f\u7528\u5806\u6808\u8ddf\u8e2a\u6765\u8c03\u8bd5\u95ee\u9898\u3002<\/li>\n<li>\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\uff08\u4f8b\u5982 <code>LogicException<\/code> \u6216 <code>RuntimeException<\/code>\uff09\u3002<\/li>\n<li>\u8c28\u614e\u4f7f\u7528 <code>finally<\/code> \u8bed\u53e5\uff0c\u56e0\u4e3a\u5b83\u4f1a\u59cb\u7ec8\u6267\u884c\uff0c\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\uff1a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5\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\uff1a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5 \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\uff1a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ PHP \u5f02\u5e38\u5904\u7406\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u901a\u8fc7 try\/catch\/finally \u6355\u83b7\u9519\u8bef\uff0c\u5e76\u53ef\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff08\u7ee7\u627f\u81ea Exception \u7c7b\uff09\u3002\u5e38\u89c1\u95ee\u9898\u5305\u62ec\uff1aPHP \u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\uff08Exception\u3001Error\u3001LogicException\u3001RuntimeException\uff09\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5f02\u5e38\u7c7b\u578b\u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\u7b49\u60c5\u51b5\uff0c\u540c\u65f6\u63d0\u4f9b\u5efa\u8bae\u4ee5\u4f7f\u7528\u7279\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b\u3001\u53d8\u91cf\u5316\u5f02\u5e38\u6d88\u606f\u548c\u8c28\u614e\u4f7f\u7528 finally \u8bed\u53e5\u3002 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\uff1a\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5 \u5728 PHP \u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u662f\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u7684\u4e00\u79cd\u673a\u5236\u3002\u5b83\u5141\u8bb8\u60a8\u4ee5\u53d7\u63a7\u548c\u4e00\u81f4\u7684\u65b9\u5f0f\u54cd\u5e94\u610f\u5916\u60c5\u51b5\u3002\u901a\u8fc7\u5904\u7406\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u63d0\u4f9b\u6709\u7528\u7684\u53cd\u9988\u3001\u8bb0\u5f55\u9519\u8bef\u8be6\u7ec6\u4fe1\u606f\uff0c\u5e76\u9632\u6b62\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5e38\u89c1\u95ee\u9898 PHP \u629b\u51fa\u4ec0\u4e48\u7c7b\u578b\u7684\u5f02\u5e38\uff1f PHP \u629b\u51fa\u5404\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u5305\u62ec\uff1a Exception\uff1a\u4e00\u822c\u5f02\u5e38\u7c7b Error\uff1a\u4e0d\u53ef\u6062\u590d\u7684\u5f02\u5e38 LogicException\uff1a\u903b\u8f91\u9519\u8bef RuntimeException\uff1a\u8fd0\u884c\u65f6\u9519\u8bef \u5982\u4f55\u6355\u83b7\u5f02\u5e38\uff1f \u4f7f\u7528 try\u3001catch \u548c finally \u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff1a try { \/\/ \u4ee3\u7801\u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38 } catch (Exception $e) { \/\/ \u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38 } finally { \/\/ \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u59cb\u7ec8\u6267\u884c\u7684\u4ee3\u7801 } \u5982\u4f55\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1f \u8981\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u8bf7\u6269\u5c55 Exception \u7c7b\uff1a class [&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-47166","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47166","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=47166"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47166\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}