{"id":405,"date":"2024-12-21T13:46:11","date_gmt":"2024-12-21T05:46:11","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=405"},"modified":"2024-12-21T13:46:11","modified_gmt":"2024-12-21T05:46:11","slug":"javascript-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/21\/javascript-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e6%8a%80%e5%b7%a7\/","title":{"rendered":"JavaScript \u9519\u8bef\u5904\u7406\u6280\u5de7"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">JavaScript \u9519\u8bef\u5904\u7406\u662f\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5065\u8fd0\u884c\u3001\u907f\u514d\u5d29\u6e83\u548c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u7684\u5173\u952e\u90e8\u5206\u3002\u9519\u8bef\u5904\u7406\u6280\u5de7\u4e0d\u4ec5\u6709\u52a9\u4e8e\u8c03\u8bd5\u548c\u4f18\u5316\u4ee3\u7801\uff0c\u8fd8\u80fd\u4f7f\u5f97\u4f60\u7684\u5e94\u7528\u5728\u9762\u5bf9\u4e0d\u53ef\u9884\u89c1\u7684\u9519\u8bef\u65f6\u4fdd\u6301\u7a33\u5b9a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684 JavaScript \u9519\u8bef\u5904\u7406\u6280\u5de7\uff1a<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 <code>try...catch<\/code> \u6355\u83b7\u540c\u6b65\u9519\u8bef<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>try...catch<\/code> \u8bed\u53e5\u7528\u4e8e\u6355\u83b7\u5e76\u5904\u7406\u4ee3\u7801\u5757\u4e2d\u7684\u540c\u6b65\u9519\u8bef\u3002\u5b83\u80fd\u5e2e\u52a9\u4f60\u907f\u514d\u5e94\u7528\u5d29\u6e83\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    \/\/ \u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801\n    let result = someFunction();\n} catch (error) {\n    \/\/ \u9519\u8bef\u5904\u7406\n    console.error(\"An error occurred:\", error);\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>try<\/code><\/strong> \u5757\u5305\u542b\u4f60\u8ba4\u4e3a\u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong><code>catch<\/code><\/strong> \u5757\u7528\u4e8e\u6355\u83b7\u5e76\u5904\u7406\u9519\u8bef\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5bf9\u8c61 <code>error<\/code> \u5305\u542b\u9519\u8bef\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7 <code>error.message<\/code> \u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u6355\u83b7\u5f02\u6b65\u9519\u8bef\uff1a<code>Promise<\/code> \u548c <code>async\/await<\/code><\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>catch<\/code> \u5904\u7406 <code>Promise<\/code> \u9519\u8bef<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u4f7f\u7528 <code>Promise<\/code> \u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>.catch()<\/code> \u6355\u83b7\u5f02\u6b65\u64cd\u4f5c\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>someAsyncFunction()\n    .then(result =&gt; {\n        console.log(\"Success:\", result);\n    })\n    .catch(error =&gt; {\n        console.error(\"Error:\", error);\n    });<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>async\/await<\/code> \u6355\u83b7\u9519\u8bef<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>async\/await<\/code> \u662f\u5904\u7406\u5f02\u6b65\u4ee3\u7801\u7684\u66f4\u73b0\u4ee3\u5316\u65b9\u6cd5\uff0c\u5b83\u8ba9\u4ee3\u7801\u770b\u8d77\u6765\u50cf\u662f\u540c\u6b65\u7684\uff0c\u4f46\u4f9d\u7136\u80fd\u591f\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async function fetchData() {\n    try {\n        let response = await fetch(\"https:\/\/api.example.com\");\n        let data = await response.json();\n        console.log(data);\n    } catch (error) {\n        console.error(\"Error fetching data:\", error);\n    }\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>try<\/code><\/strong>: \u5f02\u6b65\u4ee3\u7801\u5757\u4e2d\u53ef\u80fd\u51fa\u73b0\u9519\u8bef\u7684\u90e8\u5206\u3002<\/li>\n\n\n\n<li><strong><code>catch<\/code><\/strong>: \u6355\u83b7\u5e76\u5904\u7406\u4efb\u4f55\u7531 <code>await<\/code> \u6267\u884c\u7684\u5f02\u6b65\u64cd\u4f5c\u4e2d\u4ea7\u751f\u7684\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 <code>finally<\/code> \u8fdb\u884c\u6e05\u7406\u5de5\u4f5c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>finally<\/code> \u8bed\u53e5\u53ef\u4ee5\u5728 <code>try...catch<\/code> \u5757\u6267\u884c\u540e\u59cb\u7ec8\u8fd0\u884c\uff0c\u4e0d\u8bba\u662f\u5426\u53d1\u751f\u9519\u8bef\u3002\u8fd9\u5bf9\u4e8e\u6e05\u7406\u64cd\u4f5c\uff08\u5982\u5173\u95ed\u6587\u4ef6\u3001\u91ca\u653e\u8d44\u6e90\uff09\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    \/\/ \u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801\n    let result = someFunction();\n} catch (error) {\n    \/\/ \u9519\u8bef\u5904\u7406\n    console.error(\"An error occurred:\", error);\n} finally {\n    \/\/ \u59cb\u7ec8\u6267\u884c\u7684\u6e05\u7406\u5de5\u4f5c\n    console.log(\"This will always run, regardless of an error\");\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b\uff08\u7c7b\uff09\u6765\u589e\u5f3a\u9519\u8bef\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u5e94\u7528\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ValidationError extends Error {\n    constructor(message) {\n        super(message);\n        this.name = \"ValidationError\";\n        this.stack = (new Error()).stack;\n    }\n}\n\ntry {\n    throw new ValidationError(\"Invalid input data\");\n} catch (error) {\n    console.error(error.name); \/\/ ValidationError\n    console.error(error.message); \/\/ Invalid input data\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u80fd\u591f\u8ba9\u4f60\u5728\u5904\u7406\u9519\u8bef\u65f6\u589e\u52a0\u66f4\u591a\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u65e5\u5fd7\u8bb0\u5f55\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u6355\u83b7\u5e76\u8bb0\u5f55\u9519\u8bef<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u4e0d\u4ec5\u4ec5\u662f\u6355\u83b7\u9519\u8bef\uff0c\u8fd8\u9700\u8981\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\uff0c\u4fbf\u4e8e\u8ffd\u8e2a\u548c\u4fee\u590d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u73b0\u6210\u7684\u9519\u8bef\u76d1\u63a7\u5de5\u5177\uff0c\u6bd4\u5982 Sentry\u3001LogRocket \u6216\u8005\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u9519\u8bef\u65e5\u5fd7\u7cfb\u7edf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    \/\/ \u4ee3\u7801\u903b\u8f91\n} catch (error) {\n    logErrorToService(error);\n    alert(\"An unexpected error occurred.\");\n}\n\nfunction logErrorToService(error) {\n    \/\/ \u5c06\u9519\u8bef\u4fe1\u606f\u53d1\u9001\u5230\u670d\u52a1\u5668\u6216\u9519\u8bef\u76d1\u63a7\u670d\u52a1\n    console.log(\"Logging error:\", error);\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u907f\u514d\u5168\u5c40\u9519\u8bef\u7684\u672a\u6355\u83b7\u5f02\u5e38<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u6355\u83b7 JavaScript \u4e2d\u7684\u672a\u6355\u83b7\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>window.onerror<\/code> \u6765\u6355\u83b7\u5168\u5c40\u8303\u56f4\u5185\u7684\u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>window.onerror = function(message, source, lineno, colno, error) {\n    console.error(`Error: ${message} at ${source}:${lineno}:${colno}`);\n    \/\/ \u53ef\u4ee5\u9009\u62e9\u5c06\u9519\u8bef\u8bb0\u5f55\u5230\u670d\u52a1\u5668\u6216\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\n    return true; \/\/ \u963b\u6b62\u9ed8\u8ba4\u7684\u6d4f\u89c8\u5668\u9519\u8bef\u5904\u7406\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u4f7f\u7528 <code>try...catch<\/code> \u6355\u83b7\u5f02\u6b65\u9519\u8bef\u65f6\u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528 <code>async<\/code> \u51fd\u6570\u65f6\uff0c<code>try...catch<\/code> \u4ec5\u80fd\u6355\u83b7\u51fd\u6570\u5185\u7684\u540c\u6b65\u9519\u8bef\u3002\u5982\u679c\u5f02\u6b65\u64cd\u4f5c\u672c\u8eab\u51fa\u9519\uff0c\u9700\u8981\u786e\u4fdd\u5728\u5f02\u6b65\u51fd\u6570\u5185\u90e8\u4f7f\u7528 <code>try...catch<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async function asyncFunction() {\n    try {\n        let result = await fetchData();\n        console.log(result);\n    } catch (error) {\n        console.error(\"Error:\", error);\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u4f7f\u7528 <code>Error<\/code> \u5bf9\u8c61\u8fdb\u884c\u8c03\u8bd5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 <code>Error<\/code> \u5bf9\u8c61\uff0c\u63d0\u4f9b\u66f4\u591a\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u751a\u81f3\u5305\u62ec\u53d1\u751f\u9519\u8bef\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let error = new Error(\"Something went wrong\");\nerror.code = 500; \/\/ \u9519\u8bef\u7801\nerror.context = \"Data Fetching\"; \/\/ \u9519\u8bef\u53d1\u751f\u7684\u4e0a\u4e0b\u6587\nconsole.error(error);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>\u907f\u514d\u8fc7\u5ea6\u4f9d\u8d56\u9519\u8bef\u5904\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136\u9519\u8bef\u5904\u7406\u975e\u5e38\u91cd\u8981\uff0c\u4f46\u4e5f\u4e0d\u5e94\u8fc7\u5ea6\u4f9d\u8d56\u3002\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u5e94\u8be5\u5728\u4ee3\u7801\u4e2d\u5c3d\u53ef\u80fd\u907f\u514d\u5f15\u53d1\u9519\u8bef\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165\u9a8c\u8bc1\u3001\u5f02\u5e38\u60c5\u51b5\u7684\u5904\u7406\u548c\u4f18\u5316\u4ee3\u7801\u903b\u8f91\u6765\u9884\u9632\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>try...catch<\/code><\/strong> \u8bed\u53e5\u662f\u6355\u83b7\u540c\u6b65\u9519\u8bef\u7684\u57fa\u672c\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <strong><code>Promise<\/code><\/strong> \u548c <strong><code>async\/await<\/code><\/strong> \u6765\u5904\u7406\u5f02\u6b65\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong><code>finally<\/code><\/strong> \u7528\u4e8e\u6267\u884c\u6e05\u7406\u5de5\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u578b<\/strong> \u4f7f\u9519\u8bef\u4fe1\u606f\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/li>\n\n\n\n<li><strong>\u5168\u5c40\u9519\u8bef\u6355\u83b7<\/strong> \u53ef\u4f7f\u7528 <code>window.onerror<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u65e5\u5fd7\u8bb0\u5f55<\/strong> \u6709\u52a9\u4e8e\u751f\u4ea7\u73af\u5883\u4e2d\u8ffd\u8e2a\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u8fc7\u5ea6\u4f9d\u8d56\u9519\u8bef\u5904\u7406<\/strong>\uff0c\u5c3d\u91cf\u9632\u6b62\u9519\u8bef\u7684\u53d1\u751f\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5408\u7406\u7684\u9519\u8bef\u5904\u7406\uff0c\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u8fd8\u80fd\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u7279\u522b\u662f\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u9047\u5230\u95ee\u9898\u65f6\uff0c\u80fd\u591f\u8fc5\u901f\u54cd\u5e94\u5e76\u6062\u590d\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u9519\u8bef\u5904\u7406\u662f\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5065\u8fd0\u884c\u3001\u907f\u514d\u5d29\u6e83\u548c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u7684\u5173\u952e\u90e8\u5206\u3002\u9519\u8bef\u5904\u7406\u6280\u5de7\u4e0d\u4ec5\u6709\u52a9\u4e8e\u8c03\u8bd5 [&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-405","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/405","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=405"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/405\/revisions"}],"predecessor-version":[{"id":406,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/405\/revisions\/406"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}