{"id":21,"date":"2007-05-21T00:13:39","date_gmt":"2007-05-21T04:13:39","guid":{"rendered":"http:\/\/prglab.com\/blog\/?page_id=21"},"modified":"2007-08-25T13:59:28","modified_gmt":"2007-08-25T17:59:28","slug":"js-throw","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-throw","title":{"rendered":"JavaScript\u4e2d\u7684throw \u8bed\u53e5"},"content":{"rendered":"<p><strong>throw\u8bed\u53e5<\/strong><\/p>\n<p>\u4f7f\u7528throw\u8bed\u53e5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u4f8b\u5916(exception)\u3002\u7ed3\u5408 try&#8230;catch \u4f7f\u7528throw\u8bed\u53e5\u53ef\u4ee5\u63a7\u5236\u7a0b\u5e8f\u6d41\u7a0b\uff0c\u5e76\u751f\u6210\u51c6\u786e\u5730\u51fa\u9519\u4fe1\u606f\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">throw(exception)<\/td>\n<\/tr>\n<\/table>\n<p>\u5176\u4e2dexception\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\uff0c\u6574\u6570\uff0c\u5e03\u5c14\u503c\uff0c\u6216\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>\u6ce8\u610f throw \u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002 \u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u4f1a\u9020\u6210 JavaScript \u51fa\u9519!<\/p>\n<p><strong>\u4f8b1<\/strong><br \/>\n\u4ee5\u4e0b\u4f8b\u5b50\u6839\u636e\u53d8\u91cfx\u7684\u503c\u8fdb\u884c\u4e0d\u540c\u7684\u52a8\u4f5c\u3002  \u5982\u679cx\u503c\u5927\u4e8e10\u6216\u5c0f\u4e8e0\uff0c\u5c31\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u51fa\u9519\u4f1a\u88ab catch \u53c2\u6570\u6355\u83b7\uff0c\u663e\u793a\u9002\u5f53\u7684\u51fa\u9519\u4fe1\u606f\uff1a<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nvar x=prompt(&#8220;\u8bf7\u8f93\u5165\u4e00\u4e2a 0 \u548c 10\u4e4b\u95f4\u7684\u6570:&#8221;,&#8221;&#8221;)<br \/>\ntry<br \/>\n{<br \/>\nif(x&gt;10)<br \/>\nthrow &#8220;Err1&#8221;<br \/>\nelse if(x&lt;0)<br \/>\nthrow &#8220;Err2&#8243;<br \/>\n}<br \/>\ncatch(er)<br \/>\n{<br \/>\nif(er==&#8221;Err1&#8221;)<br \/>\nalert(&#8220;\u9519\u8bef! \u503c\u592a\u5927\u4e86&#8221;)<br \/>\nif(er == &#8220;Err2&#8221;)<br \/>\nalert(&#8220;\u9519\u8bef! \u503c\u592a\u5c0f\u4e86&#8221;)<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>throw\u8bed\u53e5 \u4f7f\u7528throw\u8bed\u53e5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u4f8b\u5916(exception)\u3002\u7ed3\u5408 try&#8230;catch \u4f7f\u7528throw\u8bed\u53e5\u53ef\u4ee5\u63a7\u5236\u7a0b\u5e8f\u6d41\u7a0b\uff0c\u5e76\u751f\u6210\u51c6\u786e\u5730\u51fa\u9519\u4fe1\u606f\u3002 \u8bed\u6cd5 throw(exception) \u5176\u4e2dexception\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\uff0c\u6574\u6570\uff0c\u5e03\u5c14\u503c\uff0c\u6216\u4e00\u4e2a\u5bf9\u8c61\u3002 \u6ce8\u610f throw \u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002 \u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u4f1a\u9020\u6210 JavaScript \u51fa\u9519! \u4f8b1 \u4ee5\u4e0b\u4f8b\u5b50\u6839\u636e\u53d8\u91cfx\u7684\u503c\u8fdb\u884c\u4e0d\u540c\u7684\u52a8\u4f5c\u3002 \u5982\u679cx\u503c\u5927\u4e8e10\u6216\u5c0f\u4e8e0\uff0c\u5c31\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u51fa\u9519\u4f1a\u88ab catch \u53c2\u6570\u6355\u83b7\uff0c\u663e\u793a\u9002\u5f53\u7684\u51fa\u9519\u4fe1\u606f\uff1a &lt;html&gt; &lt;body&gt; &lt;script type=&#8221;text\/javascript&#8221;&gt; var x=prompt(&#8220;\u8bf7\u8f93\u5165\u4e00\u4e2a 0 \u548c 10\u4e4b\u95f4\u7684\u6570:&#8221;,&#8221;&#8221;) try { if(x&gt;10) throw &#8220;Err1&#8221; else if(x&lt;0) throw &#8220;Err2&#8243; } catch(er) { if(er==&#8221;Err1&#8221;) alert(&#8220;\u9519\u8bef! \u503c\u592a\u5927\u4e86&#8221;) if(er == &#8220;Err2&#8221;) alert(&#8220;\u9519\u8bef! \u503c\u592a\u5c0f\u4e86&#8221;) } &lt;\/script&gt; &lt;\/body&gt; &lt;\/html&gt;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":14,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/21"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/21\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}