{"id":53053,"date":"2024-12-03T11:54:35","date_gmt":"2024-12-03T03:54:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53053\/"},"modified":"2024-12-03T11:54:35","modified_gmt":"2024-12-03T03:54:35","slug":"javascript-%e4%b8%ad%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53053\/","title":{"rendered":"JavaScript \u4e2d\u9519\u8bef\u548c\u5f02\u5e38\u7684\u533a\u522b"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>JavaScript \u4e2d\u9519\u8bef\u548c\u5f02\u5e38\u7684\u533a\u522b<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7c73\u4e91\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJavaScript \u4e2d\u9519\u8bef\u548c\u5f02\u5e38\u7684\u533a\u522b\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241126\/17325884276745338b80823.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u9519\u8bef\u548c\u5f02\u5e38\u7684\u533a\u522b\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u9519\u8bef\u548c\u5f02\u5e38\u7684\u533a\u522b\u63d2\u56fe\" \/><\/p>\n<blockquote>\n<p>https:\/\/github.com\/ray-d-song<\/p>\n<\/blockquote>\n<p>\u9519\u8bef\u548c\u5f02\u5e38\u662f\u4ece\u5b9e\u8df5\u4e2d\u8bde\u751f\u7684\u6982\u5ff5\uff0c\u65e8\u5728\u5904\u7406\u201c\u53ef\u7f16\u7a0b\u9519\u8bef\u201d\u3002 <\/p>\n<h2> \u9519\u8bef <\/h2>\n<p>\u4ece\u4ee3\u7801\u89d2\u5ea6\u6765\u770b\uff0c\u9519\u8bef\u5f80\u5f80\u4f1a\u88ab\u624b\u52a8\u7cbe\u786e\u5904\u7406\u3002<\/p>\n<p> \u4f8b\u5982\uff0cfna \u8c03\u7528 fnb \u548c fnc\u3002\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u80fd\u9047\u5230\u9519\u8bef\uff0c\u5904\u7406\u4ee3\u7801\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n<pre>function fna() {\n  const { err: berr, res: bres } = fnb()\n  if (berr) {\n    \/\/ ...\n    \/\/ error handling\n  }\n\n  const { err: cerr, res: cres } = fnc()\n  if (cerr) {\n    \/\/ ...\n    \/\/ error handling\n  }\n  \/\/ normal logic\n}\n<\/pre>\n<p>\u201c\u9519\u8bef\u201d\u7684\u5173\u952e\u662f\u4ece\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u6216\u6570\u7ec4\uff0c\u5176\u4e2d\u4e00\u4e2a\u5b57\u6bb5\u4ee3\u8868\u201c\u53d1\u751f\u9519\u8bef\u201d\u3002\u53ea\u8981\u8be5\u5b57\u6bb5\u4e0d\u4e3a\u7a7a\uff0c\u7a0b\u5e8f\u5458\u5c31\u77e5\u9053\u6b63\u5e38\u6d41\u7a0b\u5df2\u88ab\u4e2d\u65ad\u3002<\/p>\n<p>javascript \u6709\u4e00\u4e2a\u5185\u90e8 error \u5bf9\u8c61\u548c\u6784\u9020\u51fd\u6570\uff0c\u4f46\u8868\u793a\u9519\u8bef\u7684\u5b57\u6bb5\u4e0d\u9700\u8981\u662f error \u5bf9\u8c61\u3002\u76f8\u53cd\uff0cerror \u5bf9\u8c61\u66f4\u5e38\u7528\u4e8e\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<h2> \u4f8b\u5916 <\/h2>\n<p>\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u9519\u8bef\u5904\u7406\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981\u5f02\u5e38\uff1f <\/p>\n<p>\u60f3\u8c61\u4e00\u4e2a\u573a\u666f\uff0c\u4f60\u6709\u4e00\u4e2a\u6309\u94ae\u3002\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u4f1a\u89e6\u53d1\u51fd\u6570a\uff0c\u7ecf\u8fc7\u591a\u5c42\u8c03\u7528\uff08\u53ef\u80fd\u662f10\u5c42\uff09\uff0c\u51fd\u6570x\u51fa\u73b0\u9519\u8bef\u3002\u4f60\u4e0d\u60f3\u544a\u8bc9\u7528\u6237\u201c\u672a\u77e5\u9519\u8bef\u201d\uff0c\u800c\u662f\u60f3\u63d0\u4f9b\u6709\u5173\u95ee\u9898\u6240\u5728\u7684\u5177\u4f53\u4fe1\u606f\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u9519\u8bef\u5b9e\u73b0\u8fd9\u4e2a\u6548\u679c\uff0c\u4f46\u662f\u4f60\u9700\u8981\u5199\u5341\u6b21\u8fd9\u6837\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>function fna() {\n  const { err, res } = fnb()\n  if (err) {\n    \/\/ display error to user\n    showerr(err)\n  }\n}\n\nfunction fnb() {\n  const { err, res } = fnc()\n  if (err)\n    \/\/ propagate error\n    return { err, null }\n}\n\n\/\/ ... 10 similar passes\n\nfunction fny() {\n  const { err, res } = fnx()\n  if (err)\n    \/\/ propagate error\n    return { err, null }\n}\n<\/pre>\n<p>\u8fd9\u79cd\u6837\u677f\u4ee3\u7801\u6548\u7387\u5f88\u4f4e\u3002\u66f4\u597d\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5f02\u5e38\u3002<\/p>\n<p>\u53ea\u9700\u8981\u5728fny\u53d1\u751f\u9519\u8bef\u65f6\u629b\u51fa\u5f02\u5e38\u5373\u53ef\u3002\u5728\u9876\u5c42\uff0c\u4f60\u53ef\u4ee5\u6293\u4f4f\u5b83\u3002<\/p>\n<pre>function fna() {\n  try {\n    fnb()\n  } catch (e) {\n    showerr(e)\n  }\n}\n\n\/\/ ...\n\nfunction fny() {\n  const { err, res } = fnx()\n  if (err)\n    \/\/ \u629b\u51fa\n    throw err\n}\n<\/pre>\n<p>\u8fd9\u6837\uff0c\u65e0\u8bba\u54ea\u91cc\u53d1\u751f\u9519\u8bef\uff0c\u90fd\u53ef\u4ee5\u5728\u9876\u5c42\u6355\u83b7\uff0c\u5e76\u4e14\u5176\u4ed6\u5c42\u7684\u4ee3\u7801\u4e0d\u53d7\u5f71\u54cd\u3002<\/p>\n<p> \u907f\u514d\u4e00\u5904\u9519\u8bef\u6c61\u67d3\u6574\u4e2a\u4ee3\u7801\u7ed3\u6784\u3002<\/p>\n<h2> \u4e3a\u4ec0\u4e48\u8981\u533a\u5206\u4e24\u8005\u5462\uff1f <\/h2>\n<p>\u6211\u4eec\u5df2\u7ecf\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u9700\u8981\u5f02\u5e38\uff0c\u4f46\u662f\u4e3a\u4ec0\u4e48\u9700\u8981\u533a\u5206\u9519\u8bef\u548c\u5f02\u5e38\u5462\uff1f<\/p>\n<p>\u6700\u597d\u7684\u505a\u6cd5\u662f\u4e25\u683c\u533a\u5206\u4e24\u8005\u3002\u5982\u679c\u9519\u8bef\u4e0d\u9700\u8981\u9010\u5c42\u5411\u4e0a\u4f20\u9012\uff0c\u5219\u5e94\u76f4\u63a5\u5728\u5f53\u524d\u5c42\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982fnc\u7684\u9519\u8bef\u4e0d\u9700\u8981\u5728fna\u4e2d\u4f7f\u7528\uff0c\u6240\u4ee5\u5e94\u8be5\u76f4\u63a5\u5728b\u4e2d\u4f5c\u4e3a\u9519\u8bef\u5904\u7406\u3002<\/p>\n<p>\u5047\u8bbe\u6240\u6709\u9519\u8bef\u90fd\u5728\u9876\u5c42\u5904\u7406\uff0c\u90a3\u4e48\u6240\u6709\u903b\u8f91\u90fd\u5806\u79ef\u5728\u9876\u5c42\u7684catch\u5757\u4e2d\uff0c\u5f88\u96be\u7ef4\u62a4\u3002<\/p>\n<pre>function main() {\n  try {\n    task1()\n    task2()\n    task3()\n  } catch(e) {\n    switch(e) {\n      case \"type A\":\n        \/\/...\n        break;\n      case \"type B\":\n        \/\/...\n        break;\n      case \"type C\":\n        \/\/...\n        break;\n    }\n  }\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u518d\u6b21\u4e2d\u6807\uff01\u4f17\u5408\u79d1\u6280\u4eca\u5e74\u667a\u6167\u4ea4\u901a\u9886\u57df\u7d2f\u8ba1\u4e2d\u6807\u989d22.27\u4ebf\u5143\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u4e2d\u9519\u8bef\u548c\u5f02\u5e38\u7684\u533a\u522b \u6536\u85cf \u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7c73\u4e91\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300aJavaScript \u4e2d\u9519\u8bef\u548c\u5f02\u5e38\u7684\u533a\u522b\u300b\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 https:\/\/github.com\/ray-d-song \u9519\u8bef\u548c\u5f02\u5e38\u662f\u4ece\u5b9e\u8df5\u4e2d\u8bde\u751f\u7684\u6982\u5ff5\uff0c\u65e8\u5728\u5904\u7406\u201c\u53ef\u7f16\u7a0b\u9519\u8bef\u201d\u3002 \u9519\u8bef \u4ece\u4ee3\u7801\u89d2\u5ea6\u6765\u770b\uff0c\u9519\u8bef\u5f80\u5f80\u4f1a\u88ab\u624b\u52a8\u7cbe\u786e\u5904\u7406\u3002 \u4f8b\u5982\uff0cfna \u8c03\u7528 fnb \u548c fnc\u3002\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u80fd\u9047\u5230\u9519\u8bef\uff0c\u5904\u7406\u4ee3\u7801\u5927\u81f4\u5982\u4e0b\uff1a function fna() { const { err: berr, res: bres } = fnb() if (berr) { \/\/ &#8230; \/\/ error handling } const { err: cerr, res: cres } = fnc() if (cerr) { \/\/ &#8230; \/\/ error handling } \/\/ [&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-53053","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53053","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=53053"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53053\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}