{"id":822,"date":"2025-08-04T17:25:38","date_gmt":"2025-08-04T08:25:38","guid":{"rendered":"https:\/\/nexive.tech\/?p=822"},"modified":"2025-08-04T17:25:38","modified_gmt":"2025-08-04T08:25:38","slug":"%e3%80%90%e3%82%b5%e3%82%af%e3%83%83%e3%81%a8%e8%a7%a3%e8%aa%ac%e3%80%91php-try-catch%e6%a7%8b%e6%96%87%e3%81%a7%e4%be%8b%e5%a4%96%e5%87%a6%e7%90%86%e3%82%92%e8%a1%8c%e3%81%86%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/nexive.tech\/?p=822","title":{"rendered":"\u3010\u30b5\u30af\u30c3\u3068\u89e3\u8aac\u3011PHP try-catch\u69cb\u6587\u3067\u4f8b\u5916\u51e6\u7406\u3092\u884c\u3046\u65b9\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. \u672c\u8a18\u4e8b\u306e\u30dd\u30a4\u30f3\u30c8<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP\u306b\u304a\u3051\u308b\u4f8b\u5916\u51e6\u7406\u306e\u57fa\u672c\u69cb\u6587<code>try-catch<\/code>\u306e\u4f7f\u3044\u65b9\u3092\u89e3\u8aac<\/li>\n\n\n\n<li><code>Exception<\/code>\u30af\u30e9\u30b9\u306e\u4f7f\u3044\u65b9\u3068\u72ec\u81ea\u4f8b\u5916\u306e\u6271\u3044\u306b\u3064\u3044\u3066\u3082\u7d39\u4ecb<\/li>\n\n\n\n<li>\u5b9f\u52d9\u3067\u306e\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u8a2d\u8a08\u306e\u6ce8\u610f\u70b9\u3092\u542b\u3081\u3066\u8aac\u660e<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. PHP\u306etry-catch\u69cb\u6587\u3068\u306f\uff1f<\/h2>\n\n\n\n<p>PHP\u306e<code>try-catch<\/code>\u69cb\u6587\u306f\u3001\u4f8b\u5916\uff08Exception\uff09\u3092\u6355\u6349\u3057\u3066\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u69cb\u6587\u3067\u3059\u3002<br>\u30d5\u30a1\u30a4\u30eb\u64cd\u4f5c\u3001\u5916\u90e8API\u547c\u3073\u51fa\u3057\u3001DB\u63a5\u7d9a\u306a\u3069\u3001\u5b9f\u884c\u6642\u306b\u5931\u6557\u306e\u53ef\u80fd\u6027\u304c\u3042\u308b\u51e6\u7406\u306b\u5bfe\u3057\u3066\u3001\u5b89\u5168\u306b\u30a8\u30e9\u30fc\u51e6\u7406\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>PHP\u3067\u306f\u3001<code>try<\/code>\u30d6\u30ed\u30c3\u30af\u5185\u3067\u4f8b\u5916\u304c\u30b9\u30ed\u30fc\u3055\u308c\u308b\u3068\u3001\u5373\u5ea7\u306b<code>catch<\/code>\u30d6\u30ed\u30c3\u30af\u3078\u5236\u5fa1\u304c\u79fb\u308a\u307e\u3059\u3002<br><code>catch<\/code>\u30d6\u30ed\u30c3\u30af\u3067\u306f\u3001\u305d\u306e\u4f8b\u5916\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d7\u3051\u53d6\u308a\u3001\u30ed\u30b0\u51fa\u529b\u3084\u30e6\u30fc\u30b6\u30fc\u5411\u3051\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u306e\u8868\u793a\u306a\u3069\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001<code>throw<\/code>\u3092\u4f7f\u3063\u3066\u4efb\u610f\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u4f8b\u5916\u3092\u767a\u751f\u3055\u305b\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<br>\u4f8b\u5916\u51e6\u7406\u306fPHP5\u4ee5\u964d\u3067\u5c0e\u5165\u3055\u308c\u305f\u3082\u306e\u3067\u3001\u73fe\u4ee3\u7684\u306aPHP\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u6b20\u304b\u305b\u306a\u3044\u8a2d\u8a08\u8981\u7d20\u306e\u3072\u3068\u3064\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u8a73\u7d30\u89e3\u8aac<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u672c\u7684\u306atry-catch\u306e\u69cb\u6587<\/h3>\n\n\n\n<p>\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u306b\u5931\u6557\u3057\u305f\u5834\u5408\u306e\u4f8b\u5916\u51e6\u7406\u3092\u884c\u3046\u30b3\u30fc\u30c9\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>&lt;?php\ntry {\n    \/\/ \u5b58\u5728\u3057\u306a\u3044\u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f\n    $contents = file_get_contents('not_exist.txt');\n    echo $contents;\n} catch (Exception $e) {\n    \/\/ \u4f8b\u5916\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306e\u51e6\u7406\n    echo '\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3057\u305f: ' . $e->getMessage();\n}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">&lt;?php<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">try<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ \u5b58\u5728\u3057\u306a\u3044\u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$contents<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #DCDCAA\">file_get_contents<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&#39;not_exist.txt&#39;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$contents<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">} <\/span><span style=\"color: #C586C0\">catch<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #4EC9B0\">Exception<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ \u4f8b\u5916\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306e\u51e6\u7406<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&#39;\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3057\u305f: &#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">getMessage<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u30d5\u30a1\u30a4\u30eb\u304c\u5b58\u5728\u3057\u306a\u3044\u3068<code>Exception<\/code>\u304c\u30b9\u30ed\u30fc\u3055\u308c\u3001<code>catch<\/code>\u30d6\u30ed\u30c3\u30af\u3067\u51e6\u7406\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8907\u6570\u306e\u4f8b\u5916\u30bf\u30a4\u30d7\u3092\u51e6\u7406\u3059\u308b<\/h3>\n\n\n\n<p>\u7570\u306a\u308b\u4f8b\u5916\u30af\u30e9\u30b9\u3054\u3068\u306b\u51e6\u7406\u3092\u5206\u3051\u305f\u3044\u5834\u5408\u306f\u3001\u8907\u6570\u306e<code>catch<\/code>\u30d6\u30ed\u30c3\u30af\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>&lt;?php\ntry {\n    \/\/ \u4efb\u610f\u306e\u30ed\u30b8\u30c3\u30af\uff08\u4f8b\uff1aDB\u63a5\u7d9a\uff09\n    throw new InvalidArgumentException(\"\u5f15\u6570\u304c\u4e0d\u6b63\u3067\u3059\");\n} catch (InvalidArgumentException $e) {\n    echo \"\u5f15\u6570\u30a8\u30e9\u30fc: \" . $e->getMessage();\n} catch (Exception $e) {\n    echo \"\u305d\u306e\u4ed6\u306e\u30a8\u30e9\u30fc: \" . $e->getMessage();\n}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">&lt;?php<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">try<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ \u4efb\u610f\u306e\u30ed\u30b8\u30c3\u30af\uff08\u4f8b\uff1aDB\u63a5\u7d9a\uff09<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">throw<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">new<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">InvalidArgumentException<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;\u5f15\u6570\u304c\u4e0d\u6b63\u3067\u3059&quot;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">} <\/span><span style=\"color: #C586C0\">catch<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #4EC9B0\">InvalidArgumentException<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;\u5f15\u6570\u30a8\u30e9\u30fc: &quot;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">getMessage<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">} <\/span><span style=\"color: #C586C0\">catch<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #4EC9B0\">Exception<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;\u305d\u306e\u4ed6\u306e\u30a8\u30e9\u30fc: &quot;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">getMessage<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u30a8\u30e9\u30fc\u7a2e\u5225\u3054\u3068\u306e\u9069\u5207\u306a\u5bfe\u5fdc\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u72ec\u81ea\u306e\u4f8b\u5916\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3059\u308b<\/h3>\n\n\n\n<p>\u30c9\u30e1\u30a4\u30f3\u56fa\u6709\u306e\u30a8\u30e9\u30fc\u3092\u6271\u3046\u5834\u5408\u306f\u3001\u72ec\u81ea\u306e\u4f8b\u5916\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3059\u308b\u306e\u304c\u4e00\u822c\u7684\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1e1e1e\"><span style=\"background:#c7c7c7;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1e1e1e\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>&lt;?php\n\/\/ \u72ec\u81ea\u4f8b\u5916\u30af\u30e9\u30b9\u306e\u5b9a\u7fa9\nclass CustomException extends Exception {}\n\ntry {\n    throw new CustomException(\"\u30ab\u30b9\u30bf\u30e0\u4f8b\u5916\u304c\u767a\u751f\u3057\u307e\u3057\u305f\");\n} catch (CustomException $e) {\n    echo \"\u30ab\u30b9\u30bf\u30e0\u51e6\u7406: \" . $e->getMessage();\n} catch (Exception $e) {\n    echo \"\u305d\u306e\u4ed6\u306e\u51e6\u7406: \" . $e->getMessage();\n}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">&lt;?php<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ \u72ec\u81ea\u4f8b\u5916\u30af\u30e9\u30b9\u306e\u5b9a\u7fa9<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">CustomException<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">extends<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Exception<\/span><span style=\"color: #D4D4D4\"> {}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">try<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">throw<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">new<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">CustomException<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;\u30ab\u30b9\u30bf\u30e0\u4f8b\u5916\u304c\u767a\u751f\u3057\u307e\u3057\u305f&quot;<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">} <\/span><span style=\"color: #C586C0\">catch<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #4EC9B0\">CustomException<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;\u30ab\u30b9\u30bf\u30e0\u51e6\u7406: &quot;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">getMessage<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">} <\/span><span style=\"color: #C586C0\">catch<\/span><span style=\"color: #D4D4D4\"> (<\/span><span style=\"color: #4EC9B0\">Exception<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">echo<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;\u305d\u306e\u4ed6\u306e\u51e6\u7406: &quot;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$e<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">getMessage<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u72ec\u81ea\u30af\u30e9\u30b9\u306b\u3088\u308a\u3001\u30a8\u30e9\u30fc\u5185\u5bb9\u306e\u610f\u5473\u3065\u3051\u3084\u30ed\u30b0\u8a2d\u8a08\u304c\u660e\u78ba\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u3088\u304f\u3042\u308b\u30df\u30b9\u30fb\u8aa4\u89e3\u30fb\u843d\u3068\u3057\u7a74<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>try-catch<\/code>\u3092\u4f7f\u3063\u3066\u3082\u3001\u5168\u3066\u306e\u30a8\u30e9\u30fc\u304c\u4f8b\u5916\u306b\u306a\u308b\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<code>file_get_contents()<\/code>\u306e\u3088\u3046\u306a\u95a2\u6570\u306f\u3001\u30a8\u30e9\u30fc\u6642\u306b\u4f8b\u5916\u3067\u306f\u306a\u304f\u8b66\u544a\u3092\u767a\u3059\u308b\u5834\u5408\u3082\u3042\u308b\u305f\u3081\u3001\u660e\u793a\u7684\u306a\u30e9\u30c3\u30d1\u30fc\u304c\u5fc5\u8981\u3067\u3059\u3002<\/li>\n\n\n\n<li>PHP\u306e<code>Error<\/code>\uff08\u81f4\u547d\u7684\u30a8\u30e9\u30fc\uff09\u3068<code>Exception<\/code>\u306f\u5225\u7269\u3067\u3059\u3002PHP7\u4ee5\u964d\u306f<code>Throwable<\/code>\u3092catch\u3059\u308b\u3053\u3068\u3067\u3069\u3061\u3089\u3082\u6355\u6349\u53ef\u80fd\u3067\u3059\u304c\u3001\u5bfe\u5fdc\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n\n\n\n<li><code>catch (Exception $e)<\/code> \u306e\u4e2d\u3067\u4f8b\u5916\u3092\u518d\u30b9\u30ed\u30fc\uff08<code>throw $e<\/code>\uff09\u3059\u308b\u3053\u3068\u3067\u3001\u4e0a\u4f4d\u5c64\u3067\u518d\u51e6\u7406\u53ef\u80fd\u3067\u3059\u304c\u3001\u4f8b\u5916\u3092\u63e1\u308a\u3064\u3076\u3057\u3066\u3057\u307e\u3046\u30b3\u30fc\u30c9\u306f\u907f\u3051\u308b\u3079\u304d\u3067\u3059\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u307e\u3068\u3081<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>try-catch<\/code>\u69cb\u6587\u306f\u3001\u5b9f\u884c\u6642\u30a8\u30e9\u30fc\u3092\u5b89\u5168\u306b\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u57fa\u672c\u69cb\u6587<\/li>\n\n\n\n<li><code>Exception<\/code>\u3084\u72ec\u81ea\u4f8b\u5916\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30a8\u30e9\u30fc\u30ed\u30b8\u30c3\u30af\u3092\u660e\u78ba\u5316\u3067\u304d\u308b<\/li>\n\n\n\n<li>\u5168\u3066\u306e\u30a8\u30e9\u30fc\u304c\u4f8b\u5916\u306b\u306a\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u70b9\u306b\u6ce8\u610f\u3057\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u88dc\u52a9\u7684\u306a\u5bfe\u7b56\u3082\u691c\u8a0e\u304c\u5fc5\u8981<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u672c\u8a18\u4e8b\u306e\u30dd\u30a4\u30f3\u30c8 2. PHP\u306etry-catch\u69cb\u6587\u3068\u306f\uff1f PHP\u306etry-catch\u69cb\u6587\u306f\u3001\u4f8b\u5916\uff08Exception\uff09\u3092\u6355\u6349\u3057\u3066\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u69cb\u6587\u3067\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u64cd\u4f5c\u3001\u5916\u90e8API\u547c\u3073\u51fa\u3057\u3001DB\u63a5\u7d9a\u306a\u3069\u3001\u5b9f\u884c\u6642\u306b\u5931 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":899,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","footnotes":""},"categories":[85],"tags":[],"class_list":["post-822","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=822"}],"version-history":[{"count":2,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/822\/revisions"}],"predecessor-version":[{"id":900,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/posts\/822\/revisions\/900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=\/wp\/v2\/media\/899"}],"wp:attachment":[{"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nexive.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}