{"id":3459,"date":"2024-11-10T16:23:28","date_gmt":"2024-11-10T08:23:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3459\/"},"modified":"2024-11-10T16:23:28","modified_gmt":"2024-11-10T08:23:28","slug":"js%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3459\/","title":{"rendered":"js\u5982\u4f55\u83b7\u53d6\u5f02\u5e38"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u83b7\u53d6 javascript \u5f02\u5e38\uff1a1. try-catch \u8bed\u53e5\u7528\u4e8e\u6355\u83b7\u7279\u5b9a\u4ee3\u7801\u5757\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\uff1b2. window.onerror \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5728\u5168\u5c40\u8303\u56f4\u5185\u6355\u83b7 javascript \u9519\u8bef\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110812482538522.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u83b7\u53d6\u5f02\u5e38\u63d2\u56fe\" alt=\"js\u5982\u4f55\u83b7\u53d6\u5f02\u5e38\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u83b7\u53d6\u5f02\u5e38<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\u83b7\u53d6\u5f02\u5e38\uff1a<\/p>\n<p><strong>1. try-catch \u8bed\u53e5<\/strong><\/p>\n<p>try-catch \u8bed\u53e5\u7528\u4e8e\u6355\u83b7\u4ee3\u7801\u5757\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>try {\n  \/\/ \u4ee3\u7801\u5757\n} catch (e) {\n  \/\/ \u6355\u83b7\u5f02\u5e38\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0ce \u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u5b83\u5c06\u5b58\u50a8\u629b\u51fa\u7684\u5f02\u5e38\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>try {\n  throw new Error('\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef');\n} catch (e) {\n  console.log(e.message);  \/\/ \u8f93\u51fa: \"\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef\"\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. window.onerror \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f<\/strong><\/p>\n<p>window.onerror \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5728\u5168\u5c40\u8303\u56f4\u5185\u6355\u83b7 JavaScript \u9519\u8bef\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>window.onerror = function(message, source, lineno, colno, error) {\n  \/\/ \u6355\u83b7\u5f02\u5e38\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0cmessage \u662f\u5f02\u5e38\u6d88\u606f\uff0csource \u662f\u5f02\u5e38\u53d1\u751f\u7684\u6587\u4ef6\uff0clineno \u548c colno \u662f\u5f02\u5e38\u53d1\u751f\u7684\u4ee3\u7801\u884c\u548c\u4ee3\u7801\u5217\uff0cerror \u662f\u5f02\u5e38\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>window.onerror = function(message, source, lineno, colno, error) {\n  console.log(message);  \/\/ \u8f93\u51fa: \"\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef\"\n};\n\nthrow new Error('\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5\u4f9d\u636e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u9700\u8981\u5728\u7279\u5b9a\u4ee3\u7801\u5757\u4e2d\u6355\u83b7\u5f02\u5e38\uff0c\u4f7f\u7528 try-catch \u8bed\u53e5\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\u5728\u5168\u5c40\u8303\u56f4\u5185\u6355\u83b7\u6240\u6709 JavaScript \u9519\u8bef\uff0c\u4f7f\u7528 window.onerror \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u83b7\u53d6\u5f02\u5e38\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u83b7\u53d6 javascript \u5f02\u5e38\uff1a1. try-catch \u8bed\u53e5\u7528\u4e8e\u6355\u83b7\u7279\u5b9a\u4ee3\u7801\u5757\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\uff1b2. window.onerror \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5728\u5168\u5c40\u8303\u56f4\u5185\u6355\u83b7 javascript \u9519\u8bef\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u83b7\u53d6\u5f02\u5e38 \u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\u83b7\u53d6\u5f02\u5e38\uff1a 1. try-catch \u8bed\u53e5 try-catch \u8bed\u53e5\u7528\u4e8e\u6355\u83b7\u4ee3\u7801\u5757\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a try { \/\/ \u4ee3\u7801\u5757 } catch (e) { \/\/ \u6355\u83b7\u5f02\u5e38 } \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0ce \u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u5b83\u5c06\u5b58\u50a8\u629b\u51fa\u7684\u5f02\u5e38\u5bf9\u8c61\u3002 \u793a\u4f8b\uff1a try { throw new Error(&#8216;\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef&#8217;); } catch (e) { console.log(e.message); \/\/ \u8f93\u51fa: &#8220;\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef&#8221; } \u767b\u5f55\u540e\u590d\u5236 2. window.onerror \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f window.onerror \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u5728\u5168\u5c40\u8303\u56f4\u5185\u6355\u83b7 [&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-3459","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3459","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=3459"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3459\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}