{"id":13274,"date":"2021-02-28T09:51:19","date_gmt":"2021-02-28T00:51:19","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13274"},"modified":"2023-05-27T20:54:39","modified_gmt":"2023-05-27T11:54:39","slug":"%e3%80%90javascript%e3%80%91throw%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13274","title":{"rendered":"\u3010JavaScript\u3011\u4f8b\u5916\u51e6\u7406\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>try catch\u69cb\u6587<\/h2>\n<h3>\u69cb\u6587<\/h3>\n<pre class=\"lang:default decode:true\">try {\r\n  \u4f8b\u5916\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u547d\u4ee4\r\n} catch (\u4f8b\u5916\u3092\u53d7\u3051\u53d6\u308b\u5909\u6570) {\r\n  \u4f8b\u5916\u304c\u767a\u751f\u3057\u305f\u969b\u306b\u5b9f\u884c\u3059\u308b\u547d\u4ee4\r\n} finally {\r\n  \u4f8b\u5916\u6709\u7121\u306b\u95a2\u308f\u3089\u305a\u6700\u7d42\u7684\u306b\u5b9f\u884c\u3055\u308c\u308b\u547d\u4ee4\r\n}<\/pre>\n<p>\u3082\u3057\u3001try-catch\u69cb\u6587\u3092\u66f8\u3044\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u4f8b\u5916\u304c\u767a\u751f\u3057\u305f\u77ac\u9593\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u505c\u6b62\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<h3>\u4f8b<\/h3>\n<pre class=\"lang:default decode:true\">try{\r\n  x = 0\r\n  if (x === 0) { throw new Error('\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002'); }\r\n} catch(e) {\r\n  console.log(e.message);\r\n}<\/pre>\n<h4>throw new Error('\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8')<\/h4>\n<p>\u4f8b\u5916\u306f\u81ea\u7136\u767a\u751f\u3082\u3057\u307e\u3059\u304c\u3001\u610f\u56f3\u7684\u306b\u767a\u751f\u3055\u305b\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002\u4e0a\u8a18\u4f8b\u3067\u306fError\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f7f\u3063\u3066\u307e\u3059\u304c\u767a\u751f\u3059\u308bError\u304c\u7d5e\u308c\u3066\u3044\u308b\u306e\u3067\u3042\u308c\u3070\u4e0b\u8a18\u306e\u3088\u3046\u306a<\/p>\n<table>\n<tbody>\n<tr>\n<th>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/th>\n<th>\u539f\u56e0<\/th>\n<\/tr>\n<tr>\n<td>EvalError<\/td>\n<td>\u4e0d\u6b63\u306aeval\u95a2\u6570<\/td>\n<\/tr>\n<tr>\n<td>RangeError<\/td>\n<td>\u6307\u5b9a\u3055\u308c\u305f\u5024\u304c\u8a31\u5bb9\u7bc4\u56f2\u3092\u8d85\u3048\u3066\u3044\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td>ReferenceError<\/td>\n<td>\u5ba3\u8a00\u3055\u308c\u3066\u3044\u306a\u3044\u5909\u6570\u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u3002<\/td>\n<\/tr>\n<tr>\n<td>SyntaxError<\/td>\n<td>\u6587\u6cd5\u30a8\u30e9\u30fc<\/td>\n<\/tr>\n<tr>\n<td>TypeError<\/td>\n<td>\u6307\u5b9a\u3055\u308c\u305f\u5024\u304c\u671f\u5f85\u3055\u308c\u305f\u30c7\u30fc\u30bf\u578b\u3067\u306f\u306a\u3044\u3002<\/td>\n<\/tr>\n<tr>\n<td>URIError<\/td>\n<td>\u4e0d\u6b63\u306aURI<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>e<\/h4>\n<p>Error\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u306a\u308a\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>messege\u30d7\u30ed\u30d1\u30c6\u30a3<\/strong><\/span>\u3067\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h2>\u975e\u540c\u671f\u51e6\u7406\u306e\u5834\u5408<\/h2>\n<p>ES5\u4ee5\u964d\u306e\u69cb\u6587\u306b\u306a\u308a\u307e\u3059\u304c\u3001Promise\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306ecatch\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u308f\u306a\u3044\u3068\u4f8b\u5916\u3092\u30ad\u30e3\u30c3\u30c1\u3067\u304d\u306a\u3044\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">function hidouki() {\r\n  return new Promise((resolve, reject) =&gt; {\r\n    setTimeout(() =&gt; {\r\n      reject(\"\u30a8\u30e9\u30fc\u767a\u751f\");\r\n    }, 100);\r\n  });\r\n}\r\nhidouki().then(text =&gt; {\r\n  console.log(text);\r\n})\r\n.catch(error =&gt; {\r\n  console.log(error);\r\n});<\/pre>\n<p>\u4ed6\u306b\u3082\u975e\u540c\u671f\u306e\u4f8b\u5916\u3092\u30ad\u30e3\u30c3\u30c1\u3059\u308b\u65b9\u6cd5\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8272\u3005\u3042\u308b\u306e\u3067\u8abf\u3079\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<ul>\n<li>async catch\u3092\u4f7f\u3046\u65b9\u6cd5<\/li>\n<li>axios\u306e\u69cb\u6587\u3092\u7528\u3044\u3066catch\u3059\u308b\u65b9\u6cd5<\/li>\n<\/ul>\n<h2>\u30ad\u30e3\u30c3\u30c1\u5185\u3067\u306e\u51e6\u7406<\/h2>\n<h3>console.log\u306b\u4f8b\u5916\u5185\u5bb9\u3092\u51fa\u529b\u3059\u308b\u65b9\u6cd5<\/h3>\n<p>\u4f8b\u5916\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u4e0b\u8a18\u306e\u3088\u3046\u306b\u51fa\u529b\u3057\u307e\u3059\u3002\u3053\u308c\u3060\u3051\u3057\u3066\u3044\u308b\u306e\u306f\u3042\u307e\u308a\u307f\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<pre class=\"lang:default decode:true \">    } catch (e) {\r\n      console.log(e.message).;\r\n     }<\/pre>\n<h3>throw\u3059\u308b\u3002<\/h3>\n<p>\u6bd4\u8f03\u7684\u3088\u304f\u898b\u308b\u30d1\u30bf\u30fc\u30f3\u3067\u3059\u3002Error\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u81ea\u8eab\u3092catch\u5185\u3067\u518d\u5ea6throw\u3059\u308b\u3053\u3068\u3067\u30b9\u30bf\u30c3\u30af\u30c8\u30ec\u30fc\u30b9\u306e\u3088\u3046\u306a\u7269\u3092\u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u51fa\u529b\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">    } catch (e) {\r\n      throw e;\r\n     }<\/pre>\n<h2>\u7528\u9014<\/h2>\n<p>\u6700\u8fd1\u306eJavaScript\u958b\u767a\u3067\u306ftry-catch\u69cb\u6587\u3092\u4f7f\u7528\u3059\u308b\u5834\u9762\u3068\u3057\u3066\u306f<span style=\"color: #ff0000;\"><strong>API\u3092\u5b9f\u884c\u3059\u308b\u90e8\u5206<\/strong><\/span>\u3067\u3057\u3087\u3046\u3002API\u306f\u30a2\u30d7\u30ea\u306e\u5916\u90e8\u306e\u8a71\u306b\u306a\u308b\u306e\u3067\u5185\u90e8\u8981\u56e0\u3060\u3051\u3067\u306f\u30a8\u30e9\u30fc\u3092\u6f70\u305b\u306a\u3044\u305f\u3081\u3067\u3059\u3002<\/p>\n<h2>\u6ce8\u610f\u70b9<\/h2>\n<p>try-catch\u69cb\u6587\u3092\u4f7f\u3063\u3066\u8a18\u8ff0\u3059\u308b\u4f8b\u5916\u51e6\u7406\u306f\u304b\u306a\u308a\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u5927\u304d\u3044\u51e6\u7406\u306b\u306a\u308a\u307e\u3059\u3002\u306a\u306e\u3067\u3001\u30eb\u30fc\u30d7\u51e6\u7406\u306e\u4e2d\u306b\u8a18\u8ff0\u3059\u308b\u306a\u3069\u306f\u907f\u3051\u3066\u304a\u3044\u305f\u65b9\u304c\u7121\u96e3\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"try catch\u69cb\u6587 \u69cb\u6587 try { \u4f8b\u5916\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u547d\u4ee4 } catch (\u4f8b\u5916\u3092\u53d7\u3051\u53d6\u308b\u5909\u6570) { \u4f8b\u5916\u304c\u767a\u751f\u3057\u305f\u969b\u306b\u5b9f\u884c\u3059\u308b\u547d\u4ee4 } finally { \u4f8b\u5916\u6709\u7121\u306b\u95a2\u308f\u3089\u305a\u6700\u7d42\u7684\u306b\u5b9f\u884c\u3055\u308c\u308b\u547d\u4ee4 }  [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13274"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13274"}],"version-history":[{"count":5,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13274\/revisions"}],"predecessor-version":[{"id":20278,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13274\/revisions\/20278"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13274"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}