{"id":20,"date":"2007-05-21T00:12:43","date_gmt":"2007-05-21T04:12:43","guid":{"rendered":"http:\/\/prglab.com\/blog\/?page_id=20"},"modified":"2007-08-25T13:58:54","modified_gmt":"2007-08-25T17:58:54","slug":"js-trycatch","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-trycatch","title":{"rendered":"JavaScript\u4e2d\u7684Try&#8230;Catch \u8bed\u53e5"},"content":{"rendered":"<p>\u4f7f\u7528try&#8230;catch\u8bed\u53e5\u53ef\u4ee5\u6d4b\u8bd5\u4e00\u6bb5\u4ee3\u7801\u662f\u5426\u51fa\u9519\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n<strong>JavaScript &#8211; \u51fa\u9519\u6355\u6349<\/strong><br \/>\n\u5f53\u6d4f\u89c8\u7f51\u9875\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e38\u5e38\u4f1a\u89c1\u5230\u4e00\u4e2aJavaScript\u7684\u8b66\u544a\u7a97\u53e3\uff0c\u544a\u8bc9\u6211\u4eec\u53d1\u751f\u8fd0\u884c\u51fa\u9519\uff0c \u5e76\u63d0\u95ee\u4f60\u662f\u5426\u8981\u201cdebug?&#8221;\u3002 \u8fd9\u79cd\u51fa\u9519\u4fe1\u606f\u4e5f\u8bb8\u5bf9\u5f00\u53d1\u8005\u6709\u7528\uff0c\u4f46\u662f\u5bf9\u7528\u6237\u6ca1\u6709\u7528\u5904\u3002 \u5f53\u7528\u6237\u770b\u5230\u8fd9\u4e9b\u9519\u8bef\u65f6\uff0c\u4ed6\u4eec\u901a\u5e38\u4f1a\u79bb\u5f00\u8fd9\u4e2a\u7f51\u9875\u3002<\/p>\n<p>\u8fd9\u4e00\u7ae0\u6211\u4eec\u5c31\u4f1a\u5b66\u4e60\u600e\u6837\u6355\u6349\u548c\u5904\u7406\u8fd9\u4e9bJavaScript\u7684\u51fa\u9519\u4fe1\u606f\uff0c\u4ee5\u9632\u6b62\u56e0\u6b64\u800c\u4e22\u5931\u7528\u6237\u3002<\/p>\n<p>\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u7f51\u9875\u4e2d\u6355\u6349\u9519\u8bef\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 try&#8230;catch \u8bed\u53e5 (\u9002\u7528\u4e8eIE5\u4ee5\u4e0a, Mozilla 1.0, \u548c Netscape 6)<\/li>\n<li> \u4f7f\u7528onerror \u4e8b\u4ef6\u3002 \u8fd9\u662f\u6355\u6349\u9519\u8bef\u7684\u65e7\u7684\u4f20\u7edf\u505a\u6cd5(\u81ea\u4eceNetscape 3\u4ee5\u540e\u5c31\u5f00\u59cb\u4f7f\u7528)<\/li>\n<\/ul>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n<strong>Try&#8230;Catch\u8bed\u53e5<\/strong><br \/>\n\u4f7f\u7528try&#8230;catch\u8bed\u53e5\u53ef\u4ee5\u6d4b\u8bd5\u4e00\u6bb5\u4ee3\u7801\u662f\u5426\u51fa\u9519\u3002 try \u8bed\u53e5\u5757\u4e2d\u5305\u542b\u8981\u6267\u884c\u7684\u4ee3\u7801\uff0c catch \u8bed\u53e5\u5757\u4e2d\u5305\u542b\u5f53\u51fa\u9519\u65f6\u8981\u6267\u884c\u7684\u4ee3\u7801\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\">try<br \/>\n{<br \/>\n\/\/Run some code here<br \/>\n}<br \/>\ncatch(err)<br \/>\n{<br \/>\n\/\/Handle errors here<br \/>\n}<\/td>\n<\/tr>\n<\/table>\n<p>\u6ce8\u610f try\u548ccatch \u90fd\u662f\u5c0f\u5199\u7684\u3002 \u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u4f1a\u51fa\u9519\uff01<\/p>\n<p><strong>\u4f8b1<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4f8b\u5b50\u5305\u542b\u7684\u811a\u672c\u539f\u672c\u5e94\u8be5\u5728\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\u663e\u793a\u4fe1\u606f&#8221;\u6b22\u8fce\u8bbf\u5ba2!&#8221; \u4f46\u662f\uff0c\u56e0\u4e3a\u51fd\u6570message()\u4e2d\u6709\u4e00\u4e2a\u4e66\u5199\u9519\u8bef\uff0c alert() \u88ab\u9519\u8bef\u7684\u5199\u4e3aadddlert()\uff0c \u56e0\u6b64\u5c06\u4f1a\u4ea7\u751f\u4e00\u4e2aJavaScript \u51fa\u9519\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;head&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nfunction message()<br \/>\n{<br \/>\nadddlert(&#8220;Welcome guest!&#8221;)<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;input type=&#8221;button&#8221; value=&#8221;View message&#8221; onclick=&#8221;message()&#8221; \/&gt;<br \/>\n&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p>\u4e3a\u4e86\u5728\u51fa\u9519\u65f6\u6709\u66f4\u9002\u5f53\u7684\u53cd\u5e94\uff0c \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 try&#8230;catch \u8bed\u53e5\u3002<\/p>\n<p>\u4ee5 \u4e0b\u4f8b\u5b50\u5c06\u4e0a\u9762\u7684 &#8220;\u6b22\u8fce\u8bbf\u5ba2!&#8221; \u7684\u4f8b\u5b50\u6539\u5199\u4e3a\u4f7f\u7528try&#8230;catch \u8bed\u53e5\u3002 \u56e0\u4e3aalert() \u62fc\u5199\u9519\u8bef\uff0c \u4e00\u4e2aJavaScript\u51fa\u9519\u53d1\u751f\uff0c\u4f46\u662f\u8fd9\u6b21catch \u8bed\u53e5\u5757\u4f1a\u6355\u6349\u5230\u8fd9\u4e2a\u9519\u8bef\uff0c\u5e76\u6267\u884c\u4e00\u6bb5\u5b9a\u5236\u7684\u4ee3\u7801\u6765\u5904\u7406\u8fd9\u4e2a\u9519\u8bef\uff0c \u56e0\u6b64\u5c06\u4f1a\u663e\u793a\u4e00\u6761\u5b9a\u5236\u7684\u4fe1\u606f\u6765\u544a\u8bc9\u7528\u6237\u53d1\u751f\u4e86\u4ec0\u4e48\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;head&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nvar txt=&#8221;&#8221;<br \/>\nfunction message()<br \/>\n{<br \/>\ntry<br \/>\n{<br \/>\nadddlert(&#8220;Welcome guest!&#8221;)<br \/>\n}<br \/>\ncatch(err)<br \/>\n{<br \/>\ntxt=&#8221;There was an error on this page.\\n\\n&#8221;<br \/>\ntxt+=&#8221;Error description: &#8221; + err.description + &#8220;\\n\\n&#8221;<br \/>\ntxt+=&#8221;Click OK to continue.\\n\\n&#8221;<br \/>\nalert(txt)<br \/>\n}<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;input type=&#8221;button&#8221; value=&#8221;View message&#8221; onclick=&#8221;message()&#8221; \/&gt;<br \/>\n&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p><strong>\u4f8b2<\/strong><\/p>\n<p>\u4e0b \u9762\u8fd9\u4e2a\u4f8b\u5b50\u4f7f\u7528\u4e00\u4e2a\u786e\u8ba4\u5bf9\u8bdd\u6846\u663e\u793a\u4e00\u6761\u5b9a\u5236\u4fe1\u606f\uff0c\u544a\u8bc9\u7528\u6237\u53ef\u4ee5\u70b9\u51fbOK\u6309\u94ae\u7ee7\u7eed\u6d4f\u89c8\u7f51\u9875\uff0c\u6216\u70b9\u51fbCancel\u6309\u94ae\u6765\u8fd4\u56de\u4e3b\u9875\u3002 \u5982\u679c\u786e\u8ba4\u51fd\u6570\u8fd4\u56defalse, \u7528\u6237\u70b9\u51fb\u7684\u662fCancel, \u90a3\u4e48\u4ee3\u7801\u5c06redirect\u7528\u6237\u3002 \u5982\u679c\u786e\u8ba4\u51fd\u6570\u8fd4\u56de\u7684\u662ftrue, \u5219\u4ee3\u7801\u4ec0\u4e48\u4e5f\u4e0d\u505a\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;head&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nvar txt=&#8221;&#8221;<br \/>\nfunction message()<br \/>\n{<br \/>\ntry<br \/>\n{<br \/>\nadddlert(&#8220;Welcome guest!&#8221;)<br \/>\n}<br \/>\ncatch(err)<br \/>\n{<br \/>\ntxt=&#8221;\u672c\u9875\u9762\u51fa\u9519.\\n\\n&#8221;<br \/>\ntxt+=&#8221;\u70b9\u51fbOK\u7ee7\u7eed\u6d4f\u89c8\u672c\u9875,\\n&#8221;<br \/>\ntxt+=&#8221;\u6216\u70b9\u51fb Cancel \u8fd4\u56de\u4e3b\u9875\\n\\n&#8221;<br \/>\nif(!confirm(txt))<br \/>\n{<br \/>\ndocument.location.href=&#8221;<a href=\"http:\/\/www.shorelinesolution.com\/%22\" target=\"_blank\">http:\/\/www.shorelinesolution.com\/&#8221;<\/a><br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;input type=&#8221;button&#8221; value=&#8221;View message&#8221; onclick=&#8221;message()&#8221; \/&gt;<br \/>\n&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<strong>onerror\u4e8b\u4ef6<\/strong><\/p>\n<p>onerror\u4e8b\u4ef6\u7684\u4f7f\u7528\u4f1a\u5728\u540e\u9762\u4ecb\u7ecd\u3002\u5b66\u4e60onerror\u4e4b\u524d\u9996\u5148\u8981\u5b66\u4e60\u600e\u6837\u4f7f\u7528throw \u8bed\u53e5\u6765\u751f\u6210\u4e00\u4e2a\u4f8b\u5916(exception)\u3002 throw \u8bed\u53e5\u53ef\u4e0etry&#8230;catch\u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528try&#8230;catch\u8bed\u53e5\u53ef\u4ee5\u6d4b\u8bd5\u4e00\u6bb5\u4ee3\u7801\u662f\u5426\u51fa\u9519\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- JavaScript &#8211; \u51fa\u9519\u6355\u6349 \u5f53\u6d4f\u89c8\u7f51\u9875\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e38\u5e38\u4f1a\u89c1\u5230\u4e00\u4e2aJavaScript\u7684\u8b66\u544a\u7a97\u53e3\uff0c\u544a\u8bc9\u6211\u4eec\u53d1\u751f\u8fd0\u884c\u51fa\u9519\uff0c \u5e76\u63d0\u95ee\u4f60\u662f\u5426\u8981\u201cdebug?&#8221;\u3002 \u8fd9\u79cd\u51fa\u9519\u4fe1\u606f\u4e5f\u8bb8\u5bf9\u5f00\u53d1\u8005\u6709\u7528\uff0c\u4f46\u662f\u5bf9\u7528\u6237\u6ca1\u6709\u7528\u5904\u3002 \u5f53\u7528\u6237\u770b\u5230\u8fd9\u4e9b\u9519\u8bef\u65f6\uff0c\u4ed6\u4eec\u901a\u5e38\u4f1a\u79bb\u5f00\u8fd9\u4e2a\u7f51\u9875\u3002 \u8fd9\u4e00\u7ae0\u6211\u4eec\u5c31\u4f1a\u5b66\u4e60\u600e\u6837\u6355\u6349\u548c\u5904\u7406\u8fd9\u4e9bJavaScript\u7684\u51fa\u9519\u4fe1\u606f\uff0c\u4ee5\u9632\u6b62\u56e0\u6b64\u800c\u4e22\u5931\u7528\u6237\u3002 \u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u7f51\u9875\u4e2d\u6355\u6349\u9519\u8bef\uff1a \u4f7f\u7528 try&#8230;catch \u8bed\u53e5 (\u9002\u7528\u4e8eIE5\u4ee5\u4e0a, Mozilla 1.0, \u548c Netscape 6) \u4f7f\u7528onerror \u4e8b\u4ef6\u3002 \u8fd9\u662f\u6355\u6349\u9519\u8bef\u7684\u65e7\u7684\u4f20\u7edf\u505a\u6cd5(\u81ea\u4eceNetscape 3\u4ee5\u540e\u5c31\u5f00\u59cb\u4f7f\u7528) &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- Try&#8230;Catch\u8bed\u53e5 \u4f7f\u7528try&#8230;catch\u8bed\u53e5\u53ef\u4ee5\u6d4b\u8bd5\u4e00\u6bb5\u4ee3\u7801\u662f\u5426\u51fa\u9519\u3002 try \u8bed\u53e5\u5757\u4e2d\u5305\u542b\u8981\u6267\u884c\u7684\u4ee3\u7801\uff0c catch \u8bed\u53e5\u5757\u4e2d\u5305\u542b\u5f53\u51fa\u9519\u65f6\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002 \u8bed\u6cd5 try { \/\/Run some code here } catch(err) { \/\/Handle errors here } \u6ce8\u610f try\u548ccatch \u90fd\u662f\u5c0f\u5199\u7684\u3002 \u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u4f1a\u51fa\u9519\uff01 \u4f8b1 \u4ee5\u4e0b\u4f8b\u5b50\u5305\u542b\u7684\u811a\u672c\u539f\u672c\u5e94\u8be5\u5728\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\u663e\u793a\u4fe1\u606f&#8221;\u6b22\u8fce\u8bbf\u5ba2!&#8221; \u4f46\u662f\uff0c\u56e0\u4e3a\u51fd\u6570message()\u4e2d\u6709\u4e00\u4e2a\u4e66\u5199\u9519\u8bef\uff0c alert() \u88ab\u9519\u8bef\u7684\u5199\u4e3aadddlert()\uff0c \u56e0\u6b64\u5c06\u4f1a\u4ea7\u751f\u4e00\u4e2aJavaScript [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":13,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/20"}],"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=20"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/20\/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=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}