{"id":2055,"date":"2025-02-26T23:15:54","date_gmt":"2025-02-26T15:15:54","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2055"},"modified":"2025-02-26T23:15:54","modified_gmt":"2025-02-26T15:15:54","slug":"javascript-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%ef%bc%9athrow%e3%80%81try-%e5%92%8c-catch","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/26\/javascript-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%ef%bc%9athrow%e3%80%81try-%e5%92%8c-catch\/","title":{"rendered":"JavaScript \u9519\u8bef\u5904\u7406\uff1athrow\u3001try \u548c catch"},"content":{"rendered":"\n<p>JavaScript \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5904\u7406\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef\uff0c<code>throw<\/code>\u3001<code>try<\/code> \u548c <code>catch<\/code> \u662f\u5904\u7406\u5f02\u5e38\u7684\u6838\u5fc3\u5de5\u5177\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u9519\u8bef\uff0c\u4ee5\u4fbf\u8ba9\u7a0b\u5e8f\u66f4\u5065\u58ee\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. throw \u8bed\u53e5<\/strong><\/h3>\n\n\n\n<p><code>throw<\/code> \u8bed\u53e5\u7528\u4e8e\u629b\u51fa\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u3002\u629b\u51fa\u9519\u8bef\u540e\uff0c\u7a0b\u5e8f\u7684\u6267\u884c\u4f1a\u7acb\u5373\u505c\u6b62\uff0c\u5e76\u8f6c\u5230\u6700\u8fd1\u7684 <code>try<\/code> \u8bed\u53e5\u5757\uff08\u5982\u679c\u5b58\u5728\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8bed\u6cd5\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>throw expression;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>expression<\/code> \u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u7684\u503c\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5bf9\u8c61\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 throw<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>function checkAge(age) {\n  if (age &lt; 18) {\n    throw \"Age must be 18 or older!\";\n  }\n  return \"Age is valid!\";\n}\n\ntry {\n  console.log(checkAge(15));  \/\/ \u8fd9\u91cc\u4f1a\u629b\u51fa\u9519\u8bef\n} catch (error) {\n  console.log(\"Error: \" + error);  \/\/ \u8f93\u51fa \"Error: Age must be 18 or older!\"\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5f53 <code>checkAge(15)<\/code> \u88ab\u8c03\u7528\u65f6\uff0c\u7531\u4e8e\u5e74\u9f84\u5c0f\u4e8e 18\uff0c<code>throw<\/code> \u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\uff0c\u6355\u83b7\u7684\u9519\u8bef\u4f1a\u663e\u793a\u5728 <code>catch<\/code> \u4e2d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. try\u2026catch \u8bed\u53e5<\/strong><\/h3>\n\n\n\n<p><code>try...catch<\/code> \u8bed\u53e5\u7528\u4e8e\u6355\u83b7\u5728 <code>try<\/code> \u4ee3\u7801\u5757\u4e2d\u53d1\u751f\u7684\u9519\u8bef\u3002<code>try<\/code> \u5757\u4e2d\u662f\u4f60\u8981\u6267\u884c\u7684\u4ee3\u7801\uff0c\u5982\u679c\u4ee3\u7801\u629b\u51fa\u9519\u8bef\uff0c\u5219\u4f1a\u8df3\u8f6c\u5230 <code>catch<\/code> \u5757\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8bed\u6cd5\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n  \/\/ \u53ef\u80fd\u4f1a\u629b\u51fa\u9519\u8bef\u7684\u4ee3\u7801\n} catch (error) {\n  \/\/ \u5904\u7406\u9519\u8bef\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>error<\/code> \u662f\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\uff0c\u8868\u793a\u629b\u51fa\u7684\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 try\u2026catch<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n  let result = 10 \/ 0;  \/\/ \u8fd9\u91cc\u6ca1\u6709\u9519\u8bef\uff0c\u4f46\u53ef\u4ee5\u6a21\u62df\u5176\u4ed6\u9519\u8bef\n  console.log(result);\n} catch (error) {\n  console.log(\"An error occurred: \" + error);\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9664\u4ee5\u96f6\u4e0d\u4f1a\u629b\u51fa JavaScript \u9519\u8bef\uff08\u5b83\u8fd4\u56de <code>Infinity<\/code>\uff09\uff0c\u6240\u4ee5 <code>catch<\/code> \u5757\u4e0d\u4f1a\u6267\u884c\u3002\u901a\u5e38\uff0c<code>try...catch<\/code> \u7528\u4e8e\u6355\u83b7\u5b9e\u9645\u53ef\u80fd\u629b\u51fa\u7684\u9519\u8bef\uff08\u5982\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25\u3001\u65e0\u6548\u8f93\u5165\u7b49\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. finally \u8bed\u53e5<\/strong><\/h3>\n\n\n\n<p><code>finally<\/code> \u8bed\u53e5\u7528\u4e8e\u6307\u5b9a\u4e00\u4e2a\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u90fd\u8981\u6267\u884c\u7684\u4ee3\u7801\u5757\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u6e05\u7406\u64cd\u4f5c\uff0c\u6bd4\u5982\u5173\u95ed\u6587\u4ef6\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u8bed\u6cd5\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n  \/\/ \u53ef\u80fd\u4f1a\u629b\u51fa\u9519\u8bef\u7684\u4ee3\u7801\n} catch (error) {\n  \/\/ \u5904\u7406\u9519\u8bef\n} finally {\n  \/\/ \u65e0\u8bba\u662f\u5426\u53d1\u751f\u9519\u8bef\uff0c\u90fd\u4f1a\u6267\u884c\u7684\u4ee3\u7801\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 finally<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n  let result = 10 \/ 0;\n  console.log(result);  \/\/ \u8f93\u51fa Infinity\n} catch (error) {\n  console.log(\"An error occurred: \" + error);\n} finally {\n  console.log(\"Finally block executed!\");  \/\/ \u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u6267\u884c\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Infinity\nFinally block executed!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u6355\u83b7\u7279\u5b9a\u7684\u9519\u8bef\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p>\u5728 <code>catch<\/code> \u8bed\u53e5\u5757\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>instanceof<\/code> \u8fd0\u7b97\u7b26\u6765\u68c0\u67e5\u629b\u51fa\u7684\u9519\u8bef\u7c7b\u578b\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u7c7b\u578b\u7684\u9519\u8bef\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u6355\u83b7\u7279\u5b9a\u7c7b\u578b\u7684\u9519\u8bef<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n  let x = undefinedVariable;  \/\/ \u629b\u51fa ReferenceError \u9519\u8bef\n} catch (error) {\n  if (error instanceof ReferenceError) {\n    console.log(\"ReferenceError: Variable is not defined!\");\n  } else if (error instanceof TypeError) {\n    console.log(\"TypeError: Invalid type!\");\n  } else {\n    console.log(\"An unknown error occurred\");\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u81ea\u5b9a\u4e49\u9519\u8bef<\/strong><\/h3>\n\n\n\n<p>\u9664\u4e86\u4f7f\u7528\u9ed8\u8ba4\u7684\u9519\u8bef\u7c7b\u578b\uff0cJavaScript \u5141\u8bb8\u4f60\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u7c7b\u578b\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55 <code>Error<\/code> \u5bf9\u8c61\u6765\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u521b\u5efa\u81ea\u5b9a\u4e49\u9519\u8bef<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomError extends Error {\n  constructor(message) {\n    super(message);\n    this.name = \"CustomError\";  \/\/ \u81ea\u5b9a\u4e49\u9519\u8bef\u540d\u79f0\n  }\n}\n\ntry {\n  throw new CustomError(\"This is a custom error!\");\n} catch (error) {\n  console.log(error.name + \": \" + error.message);  \/\/ \u8f93\u51fa \"CustomError: This is a custom error!\"\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u9519\u8bef\u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<p>\u5728 <code>catch<\/code> \u5757\u4e2d\uff0c\u9519\u8bef\u5bf9\u8c61\u901a\u5e38\u5305\u542b\u4ee5\u4e0b\u5c5e\u6027\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>name<\/strong>\uff1a\u9519\u8bef\u7684\u540d\u79f0\uff08\u4f8b\u5982\uff0c<code>ReferenceError<\/code>\u3001<code>TypeError<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>message<\/strong>\uff1a\u9519\u8bef\u7684\u8be6\u7ec6\u63cf\u8ff0\u3002<\/li>\n\n\n\n<li><strong>stack<\/strong>\uff1a\u5806\u6808\u8ddf\u8e2a\uff0c\u63d0\u4f9b\u4e86\u9519\u8bef\u53d1\u751f\u7684\u4f4d\u7f6e\u548c\u8c03\u7528\u6808\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u9519\u8bef\u5bf9\u8c61<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n  let x = y;  \/\/ \u629b\u51fa ReferenceError \u9519\u8bef\n} catch (error) {\n  console.log(\"Error name: \" + error.name);  \/\/ ReferenceError\n  console.log(\"Error message: \" + error.message);  \/\/ y is not defined\n  console.log(\"Stack trace: \" + error.stack);  \/\/ \u663e\u793a\u8c03\u7528\u6808\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. \u629b\u51fa\u81ea\u5b9a\u4e49\u9519\u8bef\u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u9519\u8bef\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7 <code>throw<\/code> \u629b\u51fa\u5b83\u4eec\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function validateAge(age) {\n  if (age &lt; 18) {\n    throw new Error(\"Age must be 18 or older!\");\n  }\n  return \"Age is valid!\";\n}\n\ntry {\n  console.log(validateAge(15));  \/\/ \u629b\u51fa\u81ea\u5b9a\u4e49\u9519\u8bef\n} catch (error) {\n  console.log(error.message);  \/\/ \u8f93\u51fa \"Age must be 18 or older!\"\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>throw<\/strong>\uff1a\u7528\u4e8e\u629b\u51fa\u81ea\u5b9a\u4e49\u9519\u8bef\uff0c\u505c\u6b62\u5f53\u524d\u4ee3\u7801\u7684\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>try\u2026catch<\/strong>\uff1a\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u4ee3\u7801\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n\n\n\n<li><strong>finally<\/strong>\uff1a\u65e0\u8bba\u662f\u5426\u629b\u51fa\u9519\u8bef\uff0c\u90fd\u4f1a\u6267\u884c\u7684\u4ee3\u7801\u5757\uff0c\u9002\u5408\u7528\u4e8e\u6e05\u7406\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u5bf9\u8c61<\/strong>\uff1a\u6355\u83b7\u7684\u9519\u8bef\u5bf9\u8c61\u901a\u5e38\u5177\u6709 <code>name<\/code>\u3001<code>message<\/code> \u548c <code>stack<\/code> \u5c5e\u6027\uff0c\u5e2e\u52a9\u4f60\u8bca\u65ad\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u9519\u8bef<\/strong>\uff1a\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\u6765\u4f7f\u9519\u8bef\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u9002\u5e94\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u66f4\u7075\u6d3b\u3001\u53ef\u9760\u5730\u5904\u7406 JavaScript \u4e2d\u7684\u9519\u8bef\uff0c\u5e76\u786e\u4fdd\u4ee3\u7801\u5728\u9519\u8bef\u60c5\u51b5\u4e0b\u4f9d\u7136\u80fd\u4f18\u96c5\u5730\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5904\u7406\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef\uff0cthrow\u3001try \u548c catch \u662f\u5904\u7406\u5f02\u5e38\u7684\u6838\u5fc3\u5de5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2055","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2055"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2055\/revisions"}],"predecessor-version":[{"id":2056,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2055\/revisions\/2056"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}