{"id":46979,"date":"2024-12-02T08:43:46","date_gmt":"2024-12-02T00:43:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46979\/"},"modified":"2024-12-02T08:43:46","modified_gmt":"2024-12-02T00:43:46","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e5%9c%a8%e9%ab%98%e5%b9%b6%e5%8f%91%e5%9c%ba%e6%99%af%e4%b8%ad%e7%9a%84%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46979\/","title":{"rendered":"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u4f18\u5316"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u4f18\u5316<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u4f18\u5316\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p>\u4e3a\u4e86\u4f18\u5316 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u7f13\u5b58\u5f02\u5e38\u5bf9\u8c61\uff0c\u907f\u514d\u91cd\u590d\u521b\u5efa\u3002\u91c7\u7528\u6027\u80fd\u4f18\u5316\u7684\u5f02\u5e38\u7c7b\uff0c\u4f8b\u5982 Throwable \u7c7b\u3002\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u51fd\u6570\uff0c\u5b9e\u73b0\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236\u548c\u4f18\u5316\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729858053671b8a05034fd.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u4f18\u5316\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u4f18\u5316\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6807\u9898\uff1aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u4f18\u5316<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f00\u9500\u662f\u4e00\u4e2a\u4e0d\u5bb9\u5ffd\u89c6\u7684\u95ee\u9898\u3002PHP \u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u867d\u7136\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u5176\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u6027\u80fd\u8868\u73b0\u5374\u5e76\u4e0d\u7406\u60f3\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u79cd\u4f18\u5316 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6027\u80fd\u7684\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u5176\u6548\u679c\u3002<\/p>\n<p><strong>\u6027\u80fd\u4f18\u5316\u65b9\u6cd5<\/strong><\/p>\n<p><strong>1. \u7f13\u5b58\u5f02\u5e38\u5bf9\u8c61<\/strong><\/p>\n<p>\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u629b\u51fa\u548c\u6355\u6349\u76f8\u540c\u7684\u5f02\u5e38\u7c7b\u578b\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5f02\u5e38\u5bf9\u8c61\u7f13\u5b58\u8d77\u6765\uff0c\u907f\u514d\u6bcf\u6b21\u629b\u51fa\u90fd\u91cd\u65b0\u521b\u5efa\u5f02\u5e38\u5bf9\u8c61\uff0c\u4ece\u800c\u51cf\u5c11\u6027\u80fd\u5f00\u9500\u3002<\/p>\n<pre>\/\/ \u521b\u5efa\u5f02\u5e38\u7f13\u5b58\u6570\u7ec4\n$exceptionCache = [];\n\ntry {\n    \/\/ ... \u4ee3\u7801\u5757 ...\n} catch (RuntimeException $e) {\n    \/\/ \u5982\u679c\u5f02\u5e38\u5bf9\u8c61\u4e0d\u5b58\u5728\u4e8e\u7f13\u5b58\u6570\u7ec4\u4e2d\uff0c\u5219\u521b\u5efa\u5b83\u5e76\u7f13\u5b58\n    if (!isset($exceptionCache['RuntimeException'])) {\n        $exceptionCache['RuntimeException'] = $e;\n    }\n\n    \/\/ \u4f7f\u7528\u7f13\u5b58\u7684\u5f02\u5e38\u5bf9\u8c61\n    throw $exceptionCache['RuntimeException'];\n}<\/pre>\n<p><strong>2. \u4f7f\u7528\u6027\u80fd\u4f18\u5316\u7684\u5f02\u5e38\u7c7b<\/strong><\/p>\n<p>PHP \u5185\u7f6e\u7684 <code>Exception<\/code> \u7c7b\u6027\u80fd\u5f00\u9500\u8f83\u5927\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6027\u80fd\u4f18\u5316\u7684\u5f02\u5e38\u7c7b\uff0c\u4f8b\u5982 <code>Throwable<\/code> \u7c7b\uff0c\u6765\u51cf\u5c11\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f00\u9500\u3002<\/p>\n<pre>use Throwable;\n\nclass MyThrowable extends Throwable {}\n\ntry {\n    \/\/ ... \u4ee3\u7801\u5757 ...\n} catch (MyThrowable $e) {\n    \/\/ ... \u5904\u7406\u5f02\u5e38 ...\n}<\/pre>\n<p><strong>3. \u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u51fd\u6570<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u51fd\u6570\u6765\u66ff\u6362 PHP \u5185\u7f6e\u7684\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\uff0c\u4ee5\u5b9e\u73b0\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\u548c\u6027\u80fd\u4f18\u5316\u3002<\/p>\n<pre>set_exception_handler(function (Throwable $e) {\n    \/\/ ... \u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u903b\u8f91 ...\n});\n\ntry {\n    \/\/ ... \u4ee3\u7801\u5757 ...\n} catch (Throwable $e) {\n    \/\/ ... \u539f\u59cb\u5f02\u5e38\u5904\u7406\u903b\u8f91 ...\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u4e0a\u8ff0\u4f18\u5316\u65b9\u6cd5\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<pre>\/\/ \u7f13\u5b58\u5f02\u5e38\u5bf9\u8c61\n$exceptionCache = [];\nset_exception_handler(function (Throwable $e) {\n    \/\/ \u5982\u679c\u5f02\u5e38\u5bf9\u8c61\u4e0d\u5b58\u5728\u4e8e\u7f13\u5b58\u6570\u7ec4\u4e2d\uff0c\u5219\u521b\u5efa\u5b83\u5e76\u7f13\u5b58\n    if (!isset($exceptionCache[get_class($e)])) {\n        $exceptionCache[get_class($e)] = $e;\n    }\n\n    \/\/ \u4f7f\u7528\u7f13\u5b58\u7684\u5f02\u5e38\u5bf9\u8c61\n    throw $exceptionCache[get_class($e)];\n});\n\ntry {\n    \/\/ ... \u4ee3\u7801\u5757 ...\n} catch (RuntimeException $e) {\n    \/\/ ... \u5904\u7406\u5f02\u5e38 ...\n}\n\ntry {\n    \/\/ ... \u4ee3\u7801\u5757 ...\n} catch (MyThrowable $e) {\n    \/\/ ... \u5904\u7406\u5f02\u5e38 ...\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u672c\u6587\u4ecb\u7ecd\u7684\u4f18\u5316\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u8868\u73b0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u7b80\u5355\u6613\u884c\uff0c\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f00\u9500\uff0c\u4ece\u800c\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u7684\u6574\u4f53\u6027\u80fd\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u4f18\u5316\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u4f18\u5316 \u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u4f18\u5316\u300b\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a\u6587\u7ae0\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~ \u4e3a\u4e86\u4f18\u5316 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u7f13\u5b58\u5f02\u5e38\u5bf9\u8c61\uff0c\u907f\u514d\u91cd\u590d\u521b\u5efa\u3002\u91c7\u7528\u6027\u80fd\u4f18\u5316\u7684\u5f02\u5e38\u7c7b\uff0c\u4f8b\u5982 Throwable \u7c7b\u3002\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u51fd\u6570\uff0c\u5b9e\u73b0\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236\u548c\u4f18\u5316\u3002 \u6807\u9898\uff1aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\u7684\u6027\u80fd\u4f18\u5316 \u7b80\u4ecb \u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u7684\u6027\u80fd\u5f00\u9500\u662f\u4e00\u4e2a\u4e0d\u5bb9\u5ffd\u89c6\u7684\u95ee\u9898\u3002PHP \u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u867d\u7136\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u5176\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u6027\u80fd\u8868\u73b0\u5374\u5e76\u4e0d\u7406\u60f3\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u79cd\u4f18\u5316 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u6027\u80fd\u7684\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u5176\u6548\u679c\u3002 \u6027\u80fd\u4f18\u5316\u65b9\u6cd5 1. \u7f13\u5b58\u5f02\u5e38\u5bf9\u8c61 \u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u629b\u51fa\u548c\u6355\u6349\u76f8\u540c\u7684\u5f02\u5e38\u7c7b\u578b\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5f02\u5e38\u5bf9\u8c61\u7f13\u5b58\u8d77\u6765\uff0c\u907f\u514d\u6bcf\u6b21\u629b\u51fa\u90fd\u91cd\u65b0\u521b\u5efa\u5f02\u5e38\u5bf9\u8c61\uff0c\u4ece\u800c\u51cf\u5c11\u6027\u80fd\u5f00\u9500\u3002 \/\/ \u521b\u5efa\u5f02\u5e38\u7f13\u5b58\u6570\u7ec4 $exceptionCache = []; try { \/\/ &#8230; \u4ee3\u7801\u5757 &#8230; } catch (RuntimeException $e) { \/\/ \u5982\u679c\u5f02\u5e38\u5bf9\u8c61\u4e0d\u5b58\u5728\u4e8e\u7f13\u5b58\u6570\u7ec4\u4e2d\uff0c\u5219\u521b\u5efa\u5b83\u5e76\u7f13\u5b58 if (!isset($exceptionCache[&#8216;RuntimeException&#8217;])) { $exceptionCache[&#8216;RuntimeException&#8217;] = $e; } \/\/ \u4f7f\u7528\u7f13\u5b58\u7684\u5f02\u5e38\u5bf9\u8c61 throw $exceptionCache[&#8216;RuntimeException&#8217;]; } 2. \u4f7f\u7528\u6027\u80fd\u4f18\u5316\u7684\u5f02\u5e38\u7c7b PHP \u5185\u7f6e\u7684 Exception [&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-46979","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46979","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=46979"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46979\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}