{"id":48478,"date":"2024-12-02T09:30:44","date_gmt":"2024-12-02T01:30:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48478\/"},"modified":"2024-12-02T09:30:44","modified_gmt":"2024-12-02T01:30:44","slug":"php%e5%87%bd%e6%95%b0%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e7%9a%84%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48478\/","title":{"rendered":"PHP\u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aPHP\u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\uff1a\u4f7f\u7528 try&#8230;catch \u8bed\u53e5\u6355\u83b7\u5f02\u5e38\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u4ee5\u5b9a\u4e49\u548c\u5904\u7406\u7279\u5b9a\u9519\u8bef\u60c5\u51b5\uff0c\u4ee5\u53ca\u4f7f\u7528 finally \u5757\u6307\u5b9a\u5728\u5f02\u5e38\u53d1\u751f\u6216\u672a\u53d1\u751f\u65f6\u59cb\u7ec8\u6267\u884c\u7684\u4ee3\u7801\u3002\u8fd9\u4e9b\u5b9e\u8df5\u6709\u52a9\u4e8e\u5b9e\u73b0\u9c81\u68d2\u4e14\u53ef\u6269\u5c55\u7684\u9519\u8bef\u5904\u7406\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730035113671e3da95d450.jpg\" class=\"aligncenter\" title=\"PHP\u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u63d2\u56fe\" alt=\"PHP\u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u662f\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u4e2d\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u7684\u57fa\u672c\u673a\u5236\u3002\u5728 PHP \u4e2d\uff0c\u51fd\u6570\u5f02\u5e38\u5904\u7406\u4f7f\u6211\u4eec\u80fd\u591f\u4ee5\u7ed3\u6784\u5316\u7684\u65b9\u5f0f\u5904\u7406\u51fd\u6570\u8c03\u7528\u7684\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<p><strong>\u4f7f\u7528 try&#8230;catch \u8bed\u53e5<\/strong><\/p>\n<p><code>try...catch<\/code> \u8bed\u53e5\u7528\u4e8e\u5305\u88f9\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\u5757\uff0c\u5e76\u6307\u5b9a\u5bf9\u5f02\u5e38\u7684\u5904\u7406\u65b9\u5f0f\uff1a<\/p>\n<pre>try {\n    \/\/ \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n} catch (Exception $e) {\n    \/\/ \u6355\u83b7\u5f02\u5e38\u540e\u5904\u7406\u5f02\u5e38\u7684\u60c5\u51b5\n}<\/pre>\n<p><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/p>\n<p>PHP \u5141\u8bb8\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff0c\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9a\u4e49\u66f4\u5177\u63cf\u8ff0\u6027\u7684\u5f02\u5e38\u5e76\u4ee5\u7279\u5b9a\u65b9\u5f0f\u5904\u7406\u5b83\u4eec\uff1a<\/p>\n<pre>class MyException extends Exception {}\n\ntry {\n    \/\/ \u53ef\u80fd\u5f15\u53d1 MyException \u5f02\u5e38\u7684\u4ee3\u7801\n} catch (MyException $e) {\n    \/\/ \u5904\u7406 MyException \u5f02\u5e38\n}<\/pre>\n<p><strong>\u4f7f\u7528 finally \u5757<\/strong><\/p>\n<p><code>finally<\/code> \u5757\u7528\u4e8e\u6307\u5b9a\u5728 try \u6216 catch \u5757\u6267\u884c\u540e\u59cb\u7ec8\u6267\u884c\u7684\u4ee3\u7801\uff0c\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff1a<\/p>\n<pre>try {\n    \/\/ \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n} catch (Exception $e) {\n    \/\/ \u6355\u83b7\u5f02\u5e38\u540e\u5904\u7406\u5f02\u5e38\u7684\u60c5\u51b5\n} finally {\n    \/\/ \u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u59cb\u7ec8\u6267\u884c\u7684\u4ee3\u7801\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u7684\u51fd\u6570\uff1a<\/p>\n<pre>function uploadFile($file)\n{\n    try {\n        if (!move_uploaded_file($file['tmp_name'], 'uploads\/' . $file['name'])) {\n            throw new Exception('\u65e0\u6cd5\u4e0a\u4f20\u6587\u4ef6\u3002');\n        }\n    } catch (Exception $e) {\n        return ['error' =&gt; $e-&gt;getMessage()];\n    }\n\n    return ['success' =&gt; true];\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>uploadFile<\/code> \u51fd\u6570\u5728\u5c1d\u8bd5\u79fb\u52a8\u4e0a\u4f20\u7684\u6587\u4ef6\u65f6\u4f7f\u7528 <code>try...catch<\/code> \u8bed\u53e5\u3002\u5982\u679c\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25\uff0c\u5219\u4f1a\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002\u5f02\u5e38\u88ab\u6355\u83b7\u5e76\u4ee5 JSON \u683c\u5f0f\u8fd4\u56de\u9519\u8bef\u6d88\u606f\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728 PHP \u51fd\u6570\u4e2d\u5b9e\u73b0\u9c81\u68d2\u4e14\u53ef\u6269\u5c55\u7684\u5f02\u5e38\u5904\u7406\u3002\u901a\u8fc7\u4f7f\u7528 <code>try...catch<\/code> \u8bed\u53e5\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u548c <code>finally<\/code> \u5757\uff0c\u60a8\u53ef\u4ee5\u5728\u53d1\u751f\u5f02\u5e38\u65f6\u4e00\u81f4\u4e14\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u60c5\u51b5\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP\u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5 \u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aPHP\u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\uff1a\u4f7f\u7528 try&#8230;catch \u8bed\u53e5\u6355\u83b7\u5f02\u5e38\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u4ee5\u5b9a\u4e49\u548c\u5904\u7406\u7279\u5b9a\u9519\u8bef\u60c5\u51b5\uff0c\u4ee5\u53ca\u4f7f\u7528 finally \u5757\u6307\u5b9a\u5728\u5f02\u5e38\u53d1\u751f\u6216\u672a\u53d1\u751f\u65f6\u59cb\u7ec8\u6267\u884c\u7684\u4ee3\u7801\u3002\u8fd9\u4e9b\u5b9e\u8df5\u6709\u52a9\u4e8e\u5b9e\u73b0\u9c81\u68d2\u4e14\u53ef\u6269\u5c55\u7684\u9519\u8bef\u5904\u7406\u3002 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5 \u7b80\u4ecb \u5f02\u5e38\u5904\u7406\u662f\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u4e2d\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u7684\u57fa\u672c\u673a\u5236\u3002\u5728 PHP \u4e2d\uff0c\u51fd\u6570\u5f02\u5e38\u5904\u7406\u4f7f\u6211\u4eec\u80fd\u591f\u4ee5\u7ed3\u6784\u5316\u7684\u65b9\u5f0f\u5904\u7406\u51fd\u6570\u8c03\u7528\u7684\u5f02\u5e38\u60c5\u51b5\u3002 \u4f7f\u7528 try&#8230;catch \u8bed\u53e5 try&#8230;catch \u8bed\u53e5\u7528\u4e8e\u5305\u88f9\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\u5757\uff0c\u5e76\u6307\u5b9a\u5bf9\u5f02\u5e38\u7684\u5904\u7406\u65b9\u5f0f\uff1a try { \/\/ \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801 } catch (Exception $e) { \/\/ \u6355\u83b7\u5f02\u5e38\u540e\u5904\u7406\u5f02\u5e38\u7684\u60c5\u51b5 } \u81ea\u5b9a\u4e49\u5f02\u5e38 PHP \u5141\u8bb8\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff0c\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9a\u4e49\u66f4\u5177\u63cf\u8ff0\u6027\u7684\u5f02\u5e38\u5e76\u4ee5\u7279\u5b9a\u65b9\u5f0f\u5904\u7406\u5b83\u4eec\uff1a class MyException extends Exception {} try { \/\/ \u53ef\u80fd\u5f15\u53d1 MyException \u5f02\u5e38\u7684\u4ee3\u7801 } catch (MyException $e) { \/\/ \u5904\u7406 MyException \u5f02\u5e38 } [&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-48478","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48478","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=48478"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48478\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}