{"id":47046,"date":"2024-12-02T14:22:38","date_gmt":"2024-12-02T06:22:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47046\/"},"modified":"2024-12-02T14:22:38","modified_gmt":"2024-12-02T06:22:38","slug":"php-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%b8%ad%e7%9a%84-rethrow-%e8%a1%8c%e4%b8%ba%e5%a6%82%e4%bd%95%e7%90%86%e8%a7%a3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47046\/","title":{"rendered":"PHP \u5f02\u5e38\u5904\u7406\u4e2d\u7684 rethrow \u884c\u4e3a\u5982\u4f55\u7406\u89e3\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u5f02\u5e38\u5904\u7406\u4e2d\u7684 rethrow \u884c\u4e3a\u5982\u4f55\u7406\u89e3\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u5f02\u5e38\u5904\u7406\u4e2d\u7684 rethrow \u884c\u4e3a\u5982\u4f55\u7406\u89e3\uff1f\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p>rethrow \u884c\u4e3a\u5141\u8bb8\u5728\u6355\u83b7\u5f02\u5e38\u540e\u91cd\u65b0\u629b\u51fa\u5b83\uff0c\u5c06\u5f02\u5e38\u4f20\u64ad\u5230\u8c03\u7528\u6808\u4e2d\uff0c\u76f4\u5230\u88ab\u5904\u7406\u6216\u8fbe\u5230\u8c03\u7528\u6808\u9876\u90e8\uff0c\u5b9e\u7528\u4e8e\u9700\u8981\u5728\u4e0d\u540c\u8c03\u7528\u5c42\u6b21\u5904\u7406\u5f02\u5e38\u7684\u573a\u666f\uff0c\u4f46\u4f7f\u7528\u65f6\u5e94\u907f\u514d\u4e0d\u5fc5\u8981\u4f20\u64ad\u5f02\u5e38\uff0c\u786e\u4fdd\u4e0a\u7ea7\u8c03\u7528\u8005\u6709\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729861961671b99493efd7.jpg\" class=\"aligncenter\" title=\"PHP \u5f02\u5e38\u5904\u7406\u4e2d\u7684 rethrow \u884c\u4e3a\u5982\u4f55\u7406\u89e3\uff1f\u63d2\u56fe\" alt=\"PHP \u5f02\u5e38\u5904\u7406\u4e2d\u7684 rethrow \u884c\u4e3a\u5982\u4f55\u7406\u89e3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u7406\u89e3 PHP \u5f02\u5e38\u5904\u7406\u4e2d\u7684 rethrow \u884c\u4e3a<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u662f\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u5f02\u5e38\u548c\u9519\u8bef\u7684\u91cd\u8981\u673a\u5236\u3002\u5728 PHP \u4e2d\uff0c<code>rethrow<\/code> \u884c\u4e3a\u5141\u8bb8\u5728\u6355\u83b7\u5f02\u5e38\u540e\u91cd\u65b0\u629b\u51fa\u5b83\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>try {\n    \/\/ \u4ee3\u7801\u5757\n} catch (Exception $e) {\n    \/\/ \u5f02\u5e38\u5904\u7406\u4ee3\u7801\n    throw $e; \/\/ rethrow \u5f02\u5e38\n}<\/pre>\n<p><strong>\u5982\u4f55\u7406\u89e3 rethrow \u884c\u4e3a<\/strong><\/p>\n<p><code>rethrow<\/code> \u884c\u4e3a\u5728\u5f02\u5e38\u88ab\u6355\u83b7\u540e\uff0c\u5c06\u5176\u91cd\u65b0\u629b\u51fa\u5230\u8c03\u7528\u6808\u4e2d\u3002\u8fd9\u610f\u5473\u7740\u5f02\u5e38\u5c06\u7ee7\u7eed\u4f20\u64ad\u5230\u4e0a\u7ea7\u8c03\u7528\u8005\uff0c\u76f4\u5230\u88ab\u5904\u7406\u6216\u8fbe\u5230\u8c03\u7528\u6808\u7684\u9876\u90e8\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u5728\u4e00\u4e2a API \u4e2d\u5904\u7406\u4e00\u4e2a <code>DatabaseException<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>rethrow<\/code> \u6765\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\uff0c\u4ee5\u4fbf\u5728\u8c03\u7528\u65b9\u5904\u7406\uff1a<\/p>\n<pre>try {\n    $db = new Database();\n    $data = $db-&gt;fetch();\n} catch (DatabaseException $e) {\n    \/\/ \u8bb0\u5f55\u9519\u8bef\n    error_log($e-&gt;getMessage());\n\n    \/\/ rethrow \u5f02\u5e38\u4ee5\u4f9b\u4e0a\u7ea7\u5904\u7406\n    throw $e;\n}<\/pre>\n<p>\u5728 API \u7684\u8c03\u7528\u65b9\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406 <code>DatabaseException<\/code>\uff1a<\/p>\n<pre>try {\n    \/\/ \u8c03\u7528 API\n    $data = (new Api())-&gt;getData();\n} catch (DatabaseException $e) {\n    \/\/ \u5904\u7406\u5f02\u5e38\n    handleDatabaseError($e);\n}<\/pre>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p>\u4f7f\u7528 <code>rethrow<\/code> \u65f6\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u5b83\u5e94\u4ec5\u5728\u9700\u8981\u65f6\u4f7f\u7528\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u5730\u4f20\u64ad\u5f02\u5e38\u3002<\/li>\n<li>\u786e\u4fdd\u5728\u4e0a\u7ea7\u8c03\u7528\u8005\u4e2d\u6709\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u6765\u5904\u7406\u91cd\u65b0\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/li>\n<li>\u5982\u679c\u5f02\u5e38\u5728\u8c03\u7528\u6808\u9876\u90e8\u672a\u88ab\u5904\u7406\uff0c\u5c06\u5bfc\u81f4\u672a\u6355\u83b7\u5f02\u5e38\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPHP \u5f02\u5e38\u5904\u7406\u4e2d\u7684 rethrow \u884c\u4e3a\u5982\u4f55\u7406\u89e3\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u5f02\u5e38\u5904\u7406\u4e2d\u7684 rethrow \u884c\u4e3a\u5982\u4f55\u7406\u89e3\uff1f \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP \u5f02\u5e38\u5904\u7406\u4e2d\u7684 rethrow \u884c\u4e3a\u5982\u4f55\u7406\u89e3\uff1f\u300b\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 rethrow \u884c\u4e3a\u5141\u8bb8\u5728\u6355\u83b7\u5f02\u5e38\u540e\u91cd\u65b0\u629b\u51fa\u5b83\uff0c\u5c06\u5f02\u5e38\u4f20\u64ad\u5230\u8c03\u7528\u6808\u4e2d\uff0c\u76f4\u5230\u88ab\u5904\u7406\u6216\u8fbe\u5230\u8c03\u7528\u6808\u9876\u90e8\uff0c\u5b9e\u7528\u4e8e\u9700\u8981\u5728\u4e0d\u540c\u8c03\u7528\u5c42\u6b21\u5904\u7406\u5f02\u5e38\u7684\u573a\u666f\uff0c\u4f46\u4f7f\u7528\u65f6\u5e94\u907f\u514d\u4e0d\u5fc5\u8981\u4f20\u64ad\u5f02\u5e38\uff0c\u786e\u4fdd\u4e0a\u7ea7\u8c03\u7528\u8005\u6709\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u3002 \u7406\u89e3 PHP \u5f02\u5e38\u5904\u7406\u4e2d\u7684 rethrow \u884c\u4e3a \u5f02\u5e38\u5904\u7406\u662f\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u5f02\u5e38\u548c\u9519\u8bef\u7684\u91cd\u8981\u673a\u5236\u3002\u5728 PHP \u4e2d\uff0crethrow \u884c\u4e3a\u5141\u8bb8\u5728\u6355\u83b7\u5f02\u5e38\u540e\u91cd\u65b0\u629b\u51fa\u5b83\u3002 \u8bed\u6cd5 try { \/\/ \u4ee3\u7801\u5757 } catch (Exception $e) { \/\/ \u5f02\u5e38\u5904\u7406\u4ee3\u7801 throw $e; \/\/ rethrow \u5f02\u5e38 } \u5982\u4f55\u7406\u89e3 rethrow \u884c\u4e3a rethrow \u884c\u4e3a\u5728\u5f02\u5e38\u88ab\u6355\u83b7\u540e\uff0c\u5c06\u5176\u91cd\u65b0\u629b\u51fa\u5230\u8c03\u7528\u6808\u4e2d\u3002\u8fd9\u610f\u5473\u7740\u5f02\u5e38\u5c06\u7ee7\u7eed\u4f20\u64ad\u5230\u4e0a\u7ea7\u8c03\u7528\u8005\uff0c\u76f4\u5230\u88ab\u5904\u7406\u6216\u8fbe\u5230\u8c03\u7528\u6808\u7684\u9876\u90e8\u3002 \u5b9e\u6218\u6848\u4f8b \u5047\u8bbe\u6211\u4eec\u5728\u4e00\u4e2a API \u4e2d\u5904\u7406\u4e00\u4e2a DatabaseException\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 rethrow \u6765\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\uff0c\u4ee5\u4fbf\u5728\u8c03\u7528\u65b9\u5904\u7406\uff1a try { $db = new Database(); [&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-47046","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47046","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=47046"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47046\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}