{"id":48043,"date":"2024-12-02T15:03:53","date_gmt":"2024-12-02T07:03:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48043\/"},"modified":"2024-12-02T15:03:53","modified_gmt":"2024-12-02T07:03:53","slug":"php-%e5%87%bd%e6%95%b0%e7%9a%84%e4%bd%bf%e7%94%a8%e8%bf%87%e7%a8%8b%e4%b8%ad%e7%9a%84%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48043\/","title":{"rendered":"PHP \u51fd\u6570\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p>PHP\u51fd\u6570\u5f02\u5e38\u5904\u7406\u6307\u5357\u4f7f\u7528Exception\u7c7b\u5904\u7406\u5f02\u5e38\uff0c\u901a\u8fc7throw\u629b\u51fa\u5f02\u5e38\uff0c\u7528try-catch\u6355\u83b7\u5f02\u5e38\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ef\u7ee7\u627fException\u7c7b\u5b9e\u73b0\u66f4\u5177\u4f53\u7684\u5f02\u5e38\u7c7b\u578b\u3002\u9519\u8bef\u7ea7\u522b\u7684\u5f02\u5e38\u7531PHP\u5185\u5efa\u9519\u8bef\u89e6\u53d1\uff0c\u5982\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729951294671cf63e81aed.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6307\u5357<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u51fd\u6570\u5f02\u5e38\u5bf9\u4e8e\u5904\u7406\u610f\u5916\u60c5\u51b5\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u5065\u58ee\u6027\u3002PHP \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u672c\u6587\u5c06\u8be6\u89e3\u5176\u4f7f\u7528\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u5f02\u5e38\u7c7b<\/strong><\/p>\n<p>\u6240\u6709 PHP \u5f02\u5e38\u90fd\u7ee7\u627f\u81ea <code>Exception<\/code> \u7c7b\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ef\u4ee5\u7ee7\u627f\u81ea\u6b64\u7c7b\u5e76\u63d0\u4f9b\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p><strong>\u629b\u51fa\u5f02\u5e38<\/strong><\/p>\n<p>\u4f7f\u7528 <code>throw<\/code> \u5173\u952e\u5b57\u629b\u51fa\u5f02\u5e38\uff1a<\/p>\n<pre>throw new Exception(\"\u53d1\u751f\u4e86\u9519\u8bef\uff01\");<\/pre>\n<p><strong>\u6355\u83b7\u5f02\u5e38<\/strong><\/p>\n<p>\u4f7f\u7528 <code>try<\/code> \u548c <code>catch<\/code> \u5757\u6355\u83b7\u5f02\u5e38\uff1a<\/p>\n<pre>try {\n    \/\/ \u6267\u884c\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n} catch (Exception $e) {\n    \/\/ \u5904\u7406\u5f02\u5e38\n}<\/pre>\n<p><strong>\u5f02\u5e38\u5904\u7406\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u5728\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u53ef\u80fd\u5f15\u53d1 <code>PDOException<\/code> \u5f02\u5e38\u7684\u51fd\u6570\uff1a<\/p>\n<pre>function connectDatabase() {\n    try {\n        \/\/ \u8fde\u63a5\u6570\u636e\u5e93\u4ee3\u7801\n    } catch (PDOException $e) {\n        \/\/ \u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u5f02\u5e38\n    }\n}<\/pre>\n<p>\u5f53\u8c03\u7528 <code>connectDatabase<\/code> \u51fd\u6570\u65f6\uff0c\u5b83\u5c06\u5c1d\u8bd5\u8fde\u63a5\u6570\u636e\u5e93\u3002\u5982\u679c\u6210\u529f\uff0c\u5b83\u5c06\u900f\u660e\u5730\u7ee7\u7eed\u6267\u884c\u3002\u4f46\u5982\u679c\u8fde\u63a5\u5931\u8d25\uff0c\u5b83\u5c06\u6355\u83b7 <code>PDOException<\/code> \u5f02\u5e38\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u63aa\u65bd\uff08\u5982\u663e\u793a\u9519\u8bef\u6d88\u606f\uff09\u3002<\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u66f4\u5177\u4f53\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff1a<\/p>\n<pre>class DatabaseConnectionException extends Exception {\n    public function __construct($message) {\n        parent::__construct($message);\n    }\n}<\/pre>\n<p>\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a<\/p>\n<pre>function connectDatabase() {\n    try {\n        \/\/ \u8fde\u63a5\u6570\u636e\u5e93\u4ee3\u7801\n    } catch (PDOException $e) {\n        throw new DatabaseConnectionException($e-&gt;getMessage());\n    }\n}<\/pre>\n<p><strong>\u9519\u8bef\u7ea7\u522b\u7684\u5f02\u5e38<\/strong><\/p>\n<p>PHP 7.0 \u5f15\u5165\u4e86 <strong>\u9519\u8bef\u7ea7\u522b\u7684\u5f02\u5e38<\/strong>\u3002\u8fd9\u4e9b\u5f02\u5e38\u662f\u7531 PHP \u5185\u5efa\u9519\u8bef\uff08\u4f8b\u5982\u7c7b\u578b\u9519\u8bef\uff09\u89e6\u53d1\u7684\uff1a<\/p>\n<pre>try {\n    \/\/ \u4ea7\u751f\u7c7b\u578b\u9519\u8bef\u7684\u4ee3\u7801\n} catch (TypeError $e) {\n    \/\/ \u5904\u7406\u7c7b\u578b\u9519\u8bef\u5f02\u5e38\n}<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406 \u4f60\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587\u300aPHP \u51fd\u6570\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01 PHP\u51fd\u6570\u5f02\u5e38\u5904\u7406\u6307\u5357\u4f7f\u7528Exception\u7c7b\u5904\u7406\u5f02\u5e38\uff0c\u901a\u8fc7throw\u629b\u51fa\u5f02\u5e38\uff0c\u7528try-catch\u6355\u83b7\u5f02\u5e38\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ef\u7ee7\u627fException\u7c7b\u5b9e\u73b0\u66f4\u5177\u4f53\u7684\u5f02\u5e38\u7c7b\u578b\u3002\u9519\u8bef\u7ea7\u522b\u7684\u5f02\u5e38\u7531PHP\u5185\u5efa\u9519\u8bef\u89e6\u53d1\uff0c\u5982\u7c7b\u578b\u9519\u8bef\u3002 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6307\u5357 \u5f15\u8a00 \u51fd\u6570\u5f02\u5e38\u5bf9\u4e8e\u5904\u7406\u610f\u5916\u60c5\u51b5\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u5065\u58ee\u6027\u3002PHP \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u672c\u6587\u5c06\u8be6\u89e3\u5176\u4f7f\u7528\u8fc7\u7a0b\u3002 \u5f02\u5e38\u7c7b \u6240\u6709 PHP \u5f02\u5e38\u90fd\u7ee7\u627f\u81ea Exception \u7c7b\u3002\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ef\u4ee5\u7ee7\u627f\u81ea\u6b64\u7c7b\u5e76\u63d0\u4f9b\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u3002 \u629b\u51fa\u5f02\u5e38 \u4f7f\u7528 throw \u5173\u952e\u5b57\u629b\u51fa\u5f02\u5e38\uff1a throw new Exception(&#8220;\u53d1\u751f\u4e86\u9519\u8bef\uff01&#8221;); \u6355\u83b7\u5f02\u5e38 \u4f7f\u7528 try \u548c catch \u5757\u6355\u83b7\u5f02\u5e38\uff1a try { \/\/ \u6267\u884c\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801 } catch (Exception $e) { \/\/ \u5904\u7406\u5f02\u5e38 } \u5f02\u5e38\u5904\u7406\u793a\u4f8b \u5047\u8bbe\u6709\u4e00\u4e2a\u5728\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u53ef\u80fd\u5f15\u53d1 PDOException \u5f02\u5e38\u7684\u51fd\u6570\uff1a function connectDatabase() { try { \/\/ \u8fde\u63a5\u6570\u636e\u5e93\u4ee3\u7801 } catch (PDOException [&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-48043","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48043","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=48043"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48043\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}