{"id":1274,"date":"2026-01-06T17:36:37","date_gmt":"2026-01-06T09:36:37","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1274"},"modified":"2026-01-06T17:36:37","modified_gmt":"2026-01-06T09:36:37","slug":"php%e5%a6%82%e4%bd%95%e6%8a%9b%e5%87%ba%e5%92%8c%e6%8e%a5%e6%94%b6%e9%94%99%e8%af%af%e7%9a%84%e8%af%a6%e7%bb%86%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/01\/06\/php%e5%a6%82%e4%bd%95%e6%8a%9b%e5%87%ba%e5%92%8c%e6%8e%a5%e6%94%b6%e9%94%99%e8%af%af%e7%9a%84%e8%af%a6%e7%bb%86%e6%8c%87%e5%8d%97\/","title":{"rendered":"PHP\u5982\u4f55\u629b\u51fa\u548c\u63a5\u6536\u9519\u8bef\u7684\u8be6\u7ec6\u6307\u5357"},"content":{"rendered":"\n<p>\u5728 PHP \u4e2d\uff0c\u9519\u8bef\u5904\u7406\u662f\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002\u4e86\u89e3\u5982\u4f55 <strong>\u629b\u51fa<\/strong>\uff08throw\uff09\u548c <strong>\u63a5\u6536<\/strong>\uff08catch\uff09\u9519\u8bef\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u6709\u6548\u5730\u63a7\u5236\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\u548c\u9519\u8bef\u3002PHP \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u9519\u8bef\u548c\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u7279\u522b\u662f\u7ed3\u5408 <strong>try-catch<\/strong> \u8bed\u53e5\uff0c\u80fd\u591f\u8ba9\u6211\u4eec\u6355\u83b7\u5e76\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f <strong>PHP \u629b\u51fa\u548c\u63a5\u6536\u9519\u8bef\u7684\u8be6\u7ec6\u6307\u5357<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u57fa\u672c\u9519\u8bef\u548c\u5f02\u5e38\u7684\u6982\u5ff5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u9519\u8bef\uff08Error\uff09<\/strong><\/h3>\n\n\n\n<p>\u9519\u8bef\u901a\u5e38\u662f\u7a0b\u5e8f\u4e2d\u65e0\u6cd5\u6062\u590d\u7684\u72b6\u6001\uff0c\u53d1\u751f\u65f6\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u7ec8\u6b62\u6216\u4ea7\u751f\u4e0d\u9884\u671f\u7684\u7ed3\u679c\u3002PHP \u6709\u591a\u79cd\u7c7b\u578b\u7684\u9519\u8bef\uff0c\u5982 <strong>\u81f4\u547d\u9519\u8bef\uff08Fatal Error\uff09<\/strong>\u3001<strong>\u8b66\u544a\uff08Warning\uff09<\/strong>\u3001<strong>\u901a\u77e5\uff08Notice\uff09<\/strong> \u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u5f02\u5e38\uff08Exception\uff09<\/strong><\/h3>\n\n\n\n<p>\u5f02\u5e38\u662f\u7a0b\u5e8f\u5728\u6267\u884c\u65f6\u51fa\u73b0\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7 <strong>try-catch<\/strong> \u5757\u6355\u83b7\u5e76\u5904\u7406\u3002\u5f02\u5e38\u901a\u5e38\u7528\u6765\u8868\u793a\u53ef\u4ee5\u901a\u8fc7\u5904\u7406\u6765\u6062\u590d\u7684\u9519\u8bef\u60c5\u51b5\u3002\u4e0e\u9519\u8bef\u4e0d\u540c\uff0c\u5f02\u5e38\u53ef\u4ee5\u88ab\u6355\u83b7\u5e76\u4e14\u5904\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u629b\u51fa\u9519\u8bef\uff08throw\uff09<\/h2>\n\n\n\n<p>PHP \u4e2d\u6709\u4e24\u79cd\u5e38\u89c1\u7684\u9519\u8bef\u7c7b\u578b\u53ef\u4ee5\u629b\u51fa\uff1a<strong>\u666e\u901a\u9519\u8bef\uff08Error\uff09<\/strong> \u548c <strong>\u5f02\u5e38\uff08Exception\uff09<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u629b\u51fa\u5f02\u5e38\uff08Exception\uff09<\/strong><\/h3>\n\n\n\n<p>PHP \u4f7f\u7528 <code>throw<\/code> \u5173\u952e\u5b57\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u57fa\u672c\u8bed\u6cd5\uff1a<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nthrow new Exception(&quot;\u9519\u8bef\u4fe1\u606f&quot;, \u9519\u8bef\u4ee3\u7801);\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>new Exception()<\/code><\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5f02\u5e38\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong><code>\"\u9519\u8bef\u4fe1\u606f\"<\/code><\/strong>\uff1a\u5f02\u5e38\u7684\u6d88\u606f\uff0c\u63cf\u8ff0\u9519\u8bef\u7684\u5185\u5bb9\u3002<\/li>\n\n\n\n<li><strong><code>\u9519\u8bef\u4ee3\u7801<\/code><\/strong>\uff08\u53ef\u9009\uff09\uff1a\u4e00\u4e2a\u6574\u6570\uff0c\u4ee3\u8868\u5f02\u5e38\u7684\u81ea\u5b9a\u4e49\u4ee3\u7801\uff08\u9ed8\u8ba4\u4e3a0\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\nfunction divide($a, $b) {\n    if ($b == 0) {\n        throw new Exception(&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;, 1);\n    }\n    return $a \/ $b;\n}\n\ntry {\n    echo divide(10, 0);\n} catch (Exception $e) {\n    echo &quot;\u6355\u83b7\u5f02\u5e38: &quot; . $e-&gt;getMessage();\n}\n?&gt;\n\n<\/pre><\/div>\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\u6355\u83b7\u5f02\u5e38: \u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u629b\u51fa\u9519\u8bef\uff08Error\uff09<\/strong><\/h3>\n\n\n\n<p>\u9664\u4e86\u5f02\u5e38\uff0cPHP \u8fd8\u5141\u8bb8\u4f60\u629b\u51fa <strong>Error<\/strong> \u5bf9\u8c61\uff08\u5728 PHP 7 \u4ee5\u540e\uff0c<code>Error<\/code> \u7c7b\u4e0e <code>Exception<\/code> \u7c7b\u7c7b\u4f3c\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\nfunction checkNumber($number) {\n    if ($number &amp;lt; 0) {\n        throw new Error(&quot;\u8d1f\u6570\u4e0d\u5141\u8bb8&quot;);\n    }\n    return $number;\n}\n\ntry {\n    echo checkNumber(-5);\n} catch (Error $e) {\n    echo &quot;\u6355\u83b7\u9519\u8bef: &quot; . $e-&gt;getMessage();\n}\n?&gt;\n\n<\/pre><\/div>\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\u6355\u83b7\u9519\u8bef: \u8d1f\u6570\u4e0d\u5141\u8bb8\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u63a5\u6536\u9519\u8bef\uff08catch\uff09<\/h2>\n\n\n\n<p>PHP \u4f7f\u7528 <strong><code>try-catch<\/code><\/strong> \u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u6355\u83b7\u5f02\u5e38\uff08Exception\uff09<\/strong><\/h3>\n\n\n\n<p><code>try<\/code> \u5757\u5305\u542b\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\uff0c<code>catch<\/code> \u5757\u7528\u4e8e\u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u57fa\u672c\u8bed\u6cd5\uff1a<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntry {\n    \/\/ \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\n} catch (ExceptionType $e) {\n    \/\/ \u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\n}\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><code>ExceptionType<\/code>\uff1a\u6355\u83b7\u7684\u5f02\u5e38\u7c7b\u578b\uff08\u5982 <code>Exception<\/code>\uff09\u3002<\/li>\n\n\n\n<li><code>$e<\/code>\uff1a\u5f02\u5e38\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u83b7\u53d6\u5f02\u5e38\u7684\u4fe1\u606f\uff08\u5982\u6d88\u606f\u3001\u4ee3\u7801\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\ntry {\n    throw new Exception(&quot;\u81ea\u5b9a\u4e49\u5f02\u5e38\u4fe1\u606f&quot;);\n} catch (Exception $e) {\n    echo &quot;\u6355\u83b7\u5f02\u5e38: &quot; . $e-&gt;getMessage();\n}\n?&gt;\n\n<\/pre><\/div>\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\u6355\u83b7\u5f02\u5e38: \u81ea\u5b9a\u4e49\u5f02\u5e38\u4fe1\u606f\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u6355\u83b7\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a <code>try<\/code> \u5757\u4e2d\u6355\u83b7\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u3002PHP \u4f1a\u6839\u636e\u5f02\u5e38\u7c7b\u578b\u9009\u62e9\u5bf9\u5e94\u7684 <code>catch<\/code> \u5757\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\nclass CustomException extends Exception {}\n\ntry {\n    throw new CustomException(&quot;\u81ea\u5b9a\u4e49\u5f02\u5e38&quot;);\n} catch (CustomException $e) {\n    echo &quot;\u6355\u83b7\u81ea\u5b9a\u4e49\u5f02\u5e38: &quot; . $e-&gt;getMessage();\n} catch (Exception $e) {\n    echo &quot;\u6355\u83b7\u901a\u7528\u5f02\u5e38: &quot; . $e-&gt;getMessage();\n}\n?&gt;\n\n<\/pre><\/div>\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\u6355\u83b7\u81ea\u5b9a\u4e49\u5f02\u5e38: \u81ea\u5b9a\u4e49\u5f02\u5e38\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u591a\u91cd\u5f02\u5e38\u5904\u7406<\/strong><\/h3>\n\n\n\n<p>PHP 7 \u5141\u8bb8\u901a\u8fc7\u591a\u4e2a <code>catch<\/code> \u5757\u6355\u83b7\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u4e5f\u53ef\u4ee5\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u7c7b\u578b\u5728\u4e00\u4e2a <code>catch<\/code> \u5757\u5185\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\ntry {\n    throw new CustomException(&quot;\u81ea\u5b9a\u4e49\u5f02\u5e38&quot;);\n} catch (CustomException | Exception $e) {\n    echo &quot;\u6355\u83b7\u5f02\u5e38: &quot; . $e-&gt;getMessage();\n}\n?&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u5f02\u5e38\u5bf9\u8c61\u7684\u65b9\u6cd5<\/h2>\n\n\n\n<p>PHP \u7684 <strong>Exception<\/strong> \u548c <strong>Error<\/strong> \u5bf9\u8c61\u90fd\u63d0\u4f9b\u4e86\u82e5\u5e72\u65b9\u6cd5\uff0c\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Exception \u7c7b\u5e38\u7528\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>getMessage()<\/code><\/strong>\uff1a\u83b7\u53d6\u5f02\u5e38\u6d88\u606f\u3002<\/li>\n\n\n\n<li><strong><code>getCode()<\/code><\/strong>\uff1a\u83b7\u53d6\u5f02\u5e38\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong><code>getFile()<\/code><\/strong>\uff1a\u83b7\u53d6\u53d1\u751f\u5f02\u5e38\u7684\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong><code>getLine()<\/code><\/strong>\uff1a\u83b7\u53d6\u53d1\u751f\u5f02\u5e38\u7684\u884c\u53f7\u3002<\/li>\n\n\n\n<li><strong><code>getTrace()<\/code><\/strong>\uff1a\u83b7\u53d6\u5f02\u5e38\u7684\u5806\u6808\u8ddf\u8e2a\u3002<\/li>\n\n\n\n<li><strong><code>getTraceAsString()<\/code><\/strong>\uff1a\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u83b7\u53d6\u5806\u6808\u8ddf\u8e2a\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\ntry {\n    throw new Exception(&quot;\u53d1\u751f\u9519\u8bef&quot;, 123);\n} catch (Exception $e) {\n    echo &quot;\u9519\u8bef\u6d88\u606f: &quot; . $e-&gt;getMessage() . &quot;\\n&quot;;\n    echo &quot;\u9519\u8bef\u4ee3\u7801: &quot; . $e-&gt;getCode() . &quot;\\n&quot;;\n    echo &quot;\u6587\u4ef6: &quot; . $e-&gt;getFile() . &quot;\\n&quot;;\n    echo &quot;\u884c\u53f7: &quot; . $e-&gt;getLine() . &quot;\\n&quot;;\n    echo &quot;\u5806\u6808\u8ddf\u8e2a: &quot; . $e-&gt;getTraceAsString() . &quot;\\n&quot;;\n}\n?&gt;\n\n<\/pre><\/div>\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\u9519\u8bef\u6d88\u606f: \u53d1\u751f\u9519\u8bef\n\u9519\u8bef\u4ee3\u7801: 123\n\u6587\u4ef6: \/path\/to\/your\/script.php\n\u884c\u53f7: 10\n\u5806\u6808\u8ddf\u8e2a: ...\uff08\u5806\u6808\u8ddf\u8e2a\u5185\u5bb9\uff09\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001PHP \u9519\u8bef\u5904\u7406\u673a\u5236<\/h2>\n\n\n\n<p>\u9664\u4e86\u5f02\u5e38\u673a\u5236\uff0cPHP \u672c\u8eab\u8fd8\u6709\u4e00\u4e2a <strong>\u9519\u8bef\u5904\u7406\u673a\u5236<\/strong>\uff0c\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <strong><code>set_error_handler()<\/code><\/strong> \u6765\u5b9a\u5236\u81ea\u5df1\u7684\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570<\/strong><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\nfunction customError($errno, $errstr) {\n    echo &quot;\u9519\u8bef\u7f16\u53f7 &#x5B;$errno]: $errstr\\n&quot;;\n}\n\nset_error_handler(&quot;customError&quot;);\n\necho $undefined_variable;  \/\/ \u4f1a\u89e6\u53d1\u9519\u8bef\u5904\u7406\u51fd\u6570\n?&gt;\n\n<\/pre><\/div>\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\u9519\u8bef\u7f16\u53f7 &#x5B;8]: Undefined variable: undefined_variable\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u6355\u83b7\u81f4\u547d\u9519\u8bef\u548c\u5f02\u5e38<\/strong><\/h3>\n\n\n\n<p>\u5728 PHP 7 \u4ee5\u540e\uff0cPHP \u53ef\u4ee5\u6355\u83b7\u81f4\u547d\u9519\u8bef\uff08fatal error\uff09\u5e76\u5c06\u5176\u8f6c\u4e3a\u5f02\u5e38\uff0c\u4f7f\u7528 <strong><code>set_exception_handler()<\/code><\/strong> \u548c <strong><code>set_error_handler()<\/code><\/strong> \u7ed3\u5408\u8d77\u6765\u8fdb\u884c\u66f4\u9ad8\u6548\u7684\u9519\u8bef\u5904\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u603b\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u529f\u80fd<\/th><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><strong>\u629b\u51fa\u5f02\u5e38<\/strong><\/td><td><code>throw new Exception($message)<\/code><\/td><td>\u629b\u51fa\u5f02\u5e38\u5e76\u9644\u5e26\u9519\u8bef\u4fe1\u606f<\/td><\/tr><tr><td><strong>\u6355\u83b7\u5f02\u5e38<\/strong><\/td><td><code>try {} catch (Exception $e)<\/code><\/td><td>\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406<\/td><\/tr><tr><td><strong>\u6355\u83b7\u9519\u8bef<\/strong><\/td><td><code>set_error_handler()<\/code><\/td><td>\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\uff0c\u6355\u83b7\u8fd0\u884c\u65f6\u9519\u8bef<\/td><\/tr><tr><td><strong>\u5f02\u5e38\u4fe1\u606f<\/strong><\/td><td><code>getMessage()<\/code> \u7b49<\/td><td>\u83b7\u53d6\u5f02\u5e38\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5982\u6d88\u606f\u3001\u4ee3\u7801\u3001\u5806\u6808\u8ddf\u8e2a\u7b49<\/td><\/tr><tr><td><strong>\u591a\u4e2a\u5f02\u5e38<\/strong><\/td><td>`catch (Exception<\/td><td>CustomException)`<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528 PHP \u7684 <strong>\u5f02\u5e38\u5904\u7406\u673a\u5236<\/strong>\uff0c\u4f60\u53ef\u4ee5\u4f18\u96c5\u5730\u6355\u83b7\u548c\u5904\u7406\u9519\u8bef\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u80fd\u591f\u7a33\u5065\u5730\u8fd0\u884c\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u6df1\u5165\u7684\u7406\u89e3\u6216\u8005\u60f3\u8981\u6211\u4e3a\u4f60\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u9519\u8bef\u5904\u7406\u6846\u67b6\uff0c\u968f\u65f6\u544a\u8bc9\u6211\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PHP \u4e2d\uff0c\u9519\u8bef\u5904\u7406\u662f\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002\u4e86\u89e3\u5982\u4f55 &#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/01\/06\/php%e5%a6%82%e4%bd%95%e6%8a%9b%e5%87%ba%e5%92%8c%e6%8e%a5%e6%94%b6%e9%94%99%e8%af%af%e7%9a%84%e8%af%a6%e7%bb%86%e6%8c%87%e5%8d%97\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-1274","post","type-post","status-publish","format-standard","hentry","category-php"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1274"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1274\/revisions"}],"predecessor-version":[{"id":1275,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1274\/revisions\/1275"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}