{"id":369,"date":"2020-11-03T12:19:23","date_gmt":"2020-11-03T16:19:23","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/?page_id=369"},"modified":"2020-11-03T12:19:23","modified_gmt":"2020-11-03T16:19:23","slug":"exception-handling","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/cplusplus-tutorial\/advanced-concepts\/exception-handling","title":{"rendered":"\u51fa\u9519\u5904\u7406"},"content":{"rendered":"\n<h2>5.3 \u51fa\u9519\u5904\u7406 (Exception handling)<\/h2>\n\n\n\n<p><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/new-in-ansi.png\"><\/p>\n\n\n\n<p>\u672c\u8282\u4ecb\u7ecd\u7684\u51fa\u9519\u5904\u7406\u662fANSI-C++ \u6807\u51c6\u5f15\u5165\u7684\u65b0\u529f\u80fd\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684C++ \u7f16\u8bd1\u5668\u4e0d\u517c\u5bb9\u8fd9\u4e2a\u6807\u51c6\uff0c\u5219\u4f60\u53ef\u80fd\u65e0\u6cd5\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u5728\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\uff0c\u5f88\u591a\u65f6\u5019\u6211\u4eec\u662f\u65e0\u6cd5\u786e\u5b9a\u4e00\u6bb5\u4ee3\u7801\u662f\u5426\u603b\u662f\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u7684\uff0c\u6216\u8005\u56e0\u4e3a\u7a0b\u5e8f\u8bbf\u95ee\u4e86\u5e76\u4e0d\u5b58\u5728\u7684\u8d44\u6e90\uff0c\u6216\u8005\u7531\u4e8e\u4e00\u4e9b\u53d8\u91cf\u8d85\u51fa\u4e86\u9884\u671f\u7684\u8303\u56f4\uff0c\u7b49\u7b49\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e9b\u60c5\u51b5\u6211\u4eec\u7edf\u79f0\u4e3a\u51fa\u9519\uff08\u4f8b\u5916\uff09\uff0cC++ \u65b0\u8fd1\u5f15\u5165\u7684\u4e09\u79cd\u64cd\u4f5c\u7b26\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u8fd9\u4e9b\u51fa\u9519\u60c5\u51b5\uff1a try, throw \u548c catch \u3002<\/p>\n\n\n\n<p>\u5b83\u4eec\u7684\u4e00\u822c\u7528\u6cd5\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\t<strong>try {<\/strong>\n\t    \/\/ code to be tried\n\t    <strong>throw<\/strong> exception;<br>\t<strong>}<\/strong>\n\t<strong>catch (<\/strong>type  exception<strong>)<br>\t{<\/strong>\n\t    \/\/ code to be executed in case of exception\n\t<strong>}<\/strong><\/pre>\n\n\n\n<p>\u5b83\u4eec\u6240\u8fdb\u884c\u7684\u64cd\u4f5c\u662f\uff1a<\/p>\n\n\n\n<ul><li>try \u8bed\u53e5\u5757\u4e2d\u7684\u4ee3\u7801\u88ab\u6b63\u5e38\u6267\u884c\u3002\u5982\u679c\u6709\u4f8b\u5916\u53d1\u751f\uff0c\u4ee3\u7801\u5fc5\u987b\u4f7f\u7528\u5173\u952e\u5b57throw \u548c\u4e00\u4e2a\u53c2\u6570\u6765\u6254\u51fa\u4e00\u4e2a\u4f8b\u5916\u3002\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u4efb\u4f55\u6709\u6548\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u7684\u7c7b\u578b\u53cd\u6620\u4e86\u4f8b\u5916\u7684\u7279\u5f81\u3002<\/li><li>\u5982\u679c\u6709\u4f8b\u5916\u53d1\u751f\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728try \u8bed\u53e5\u5757\u4e2d\u6709\u4e00\u4e2athrow \u6307\u4ee4\u88ab\u6267\u884c\u4e86\uff0c\u5219catch \u8bed\u53e5\u5757\u4f1a\u88ab\u6267\u884c\uff0c\u7528\u6765\u63a5\u6536throw\u4f20\u6765\u7684\u4f8b\u5916\u53c2\u6570\u3002<\/li><\/ul>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ exceptions #include &lt;iostream.h&gt; int main () { char myarray[10]; try { for (int n=0; n&lt;=10; n++) { if (n&gt;9) throw &#8220;Out of range&#8221;; myarray[n]=&#8217;z&#8217;; } } catch (char * str) { cout &lt;&lt; &#8220;Exception: &#8221; &lt;&lt; str &lt;&lt; endl; } return 0; }<\/td><td>Exception: Out of range<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u5728n \u5faa\u73af\u4e2d\uff0cn \u53d8\u7684\u5927\u4e8e9 \u4e86\uff0c\u5219\u4ecd\u51fa\u4e00\u4e2a\u4f8b\u5916\uff0c\u56e0\u4e3a\u6570\u7ec4 myarray[n] \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u6307\u5411\u4e00\u4e2a\u65e0\u6548\u7684\u5185\u5b58\u5730\u5740\u3002\u5f53throw \u88ab\u6267\u884c\u7684\u65f6\u5019\uff0ctry \u8bed\u53e5\u5757\u7acb\u5373\u88ab\u505c\u6b62\u6267\u884c\uff0c\u5728try \u8bed\u53e5\u5757\u4e2d\u751f\u6210\u7684\u6240\u6709\u5bf9\u8c61\u4f1a\u88ab\u9500\u6bc1\u3002\u6b64\u540e\uff0c\u63a7\u5236\u6743\u88ab\u4f20\u9012\u7ed9\u76f8\u5e94\u7684catch \u8bed\u53e5\u5757\uff08\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u5373\u6267\u884c\u4ec5\u6709\u7684\u4e00\u4e2acatch\uff09\u3002\u6700\u540e\u7a0b\u5e8f\u7d27\u8ddf\u7740catch \u8bed\u53e5\u5757\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u5c31\u662f\u6267\u884c return 0;.<\/p>\n\n\n\n<p>throw \u8bed\u6cd5\u4e0e return \u76f8\u4f3c\uff0c\u53ea\u662f\u53c2\u6570\u4e0d\u9700\u8981\u6269\u5728\u62ec\u53f7\u3002<\/p>\n\n\n\n<p>catch \u8bed\u53e5\u5757\u5fc5\u987b\u7d27\u8ddf\u7740try \u8bed\u53e5\u5757\u540e\u9762\uff0c\u4e2d\u95f4\u4e0d\u80fd\u6709\u5176\u5b83\u7684\u4ee3\u7801\u3002catch \u6355\u83b7\u7684\u53c2\u6570\u53ef\u4ee5\u662f\u4efb\u4f55\u6709\u6548\u7684\u6570\u636e\u7c7b\u578b\u3002catch \u751a\u81f3\u53ef\u4ee5\u88ab\u91cd\u8f7d\u4ee5\u4fbf\u80fd\u591f\u63a5\u53d7\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u88ab\u6267\u884ccatch \u8bed\u53e5\u5757\u662f\u76f8\u5e94\u7684\u7b26\u5408\u88abthrow\u6254\u51fa\u7684\u53c2\u6570\u7c7b\u578b\u7684\u90a3\u4e00\u4e2a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ exceptions: multiple catch blocks #include &lt;iostream.h&gt; int main () { try { char * mystring; mystring = new char [10]; if (mystring == NULL) throw &#8220;Allocation failure&#8221;; for (int n=0; n&lt;=100; n++) { if (n&gt;9) throw n; mystring[n]=&#8217;z&#8217;; } } catch (int i) { cout &lt;&lt; &#8220;Exception: &#8220;; cout &lt;&lt; &#8220;index &#8221; &lt;&lt; i &lt;&lt; &#8221; is out of range&#8221; &lt;&lt; endl; } catch (char * str) { cout &lt;&lt; &#8220;Exception: &#8221; &lt;&lt; str &lt;&lt; endl; } return 0; }<\/td><td>Exception: index 10 is out of range<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u4f8b\u5916\u53ef\u80fd\u53d1\u751f\uff1a<\/p>\n\n\n\n<ol><li>\u5982\u679c\u8981\u6c42\u768410\u4e2a\u5b57\u7b26\u7a7a\u95f4\u4e0d\u80fd\u591f\u88ab\u8d4b\u503c\uff08\u8fd9\u79cd\u60c5\u51b5\u5f88\u5c11\uff0c\u4f46\u662f\u6709\u53ef\u80fd\u53d1\u751f\uff09\uff1a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6254\u51fa\u7684\u4f8b\u5916\u4f1a\u88abcatch (char * str)\u6355\u83b7\u3002<\/li><li>n\u8d85\u8fc7\u4e86mystring\u7684\u6700\u5927\u7d22\u5f15\u503c(index)\uff1a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6254\u51fa\u7684\u4f8b\u5916\u5c06\u88abcatch (int i)\u6355\u83b7\uff0c\u56e0\u4e3a\u5b83\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u6574\u578b\u503c\u3002<\/li><\/ol>\n\n\n\n<p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2acatch \u8bed\u53e5\u5757\u6765\u6355\u83b7\u6240\u6709\u7684\u4f8b\u5916\uff0c\u4e0d\u8bba\u6254\u51fa\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u53c2\u6570\u3002\u8fd9\u79cd\u60c5\u51b5\u6211\u4eec\u9700\u8981\u5728catch \u6216\u9762\u7684\u62ec\u53f7\u4e2d\u5199\u4e09\u4e2a\u70b9\u6765\u4ee3\u66ff\u539f\u6765\u7684\u53c2\u6570\u7c7b\u578b\u548c\u540d\u79f0\uff0c\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   try {<br>       \/\/ code here\n   }\n   catch (...) {\n       cout &lt;&lt; \"Exception occurred\";\n   }<\/pre>\n\n\n\n<p>try-catch \u4e5f\u662f\u53ef\u4ee5\u88ab\u5d4c\u5957\u4f7f\u7528\u7684\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8868\u8fbe\u5f0fthrow;(\u4e0d\u5e26\u53c2\u6570)\u5c06\u91cc\u9762\u7684catch \u8bed\u53e5\u5757\u6355\u83b7\u7684\u4f8b\u5916\u4f20\u9012\u5230\u5916\u9762\u4e00\u5c42\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   try {<br>       try {<br>           \/\/ code here\n       }\n       catch (int n) {\n           throw;\n       }\n   }\n   catch (...) {\n       cout &lt;&lt; \"Exception occurred\";\n   }<\/pre>\n\n\n\n<h3>\u6ca1\u6709\u6355\u83b7\u7684\u4f8b\u5916 (Exceptions not caught)<\/h3>\n\n\n\n<p>\u5982\u679c\u7531\u4e8e\u6ca1\u6709\u5bf9\u5e94\u7684\u7c7b\u578b\uff0c\u4e00\u4e2a\u4f8b\u5916\u6ca1\u6709\u88ab\u4efb\u4f55catch \u8bed\u53e5\u6355\u83b7\uff0c\u7279\u6b8a\u51fd\u6570terminate \u5c06\u88ab\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u901a\u5e38\u5df2\u88ab\u5b9a\u4e49\u4e86\uff0c\u4ee5\u4fbf\u7acb\u5373\u7ed3\u675f\u5f53\u524d\u7684\u8fdb\u7a0b(process)\uff0c\u5e76\u663e\u793a\u4e00\u4e2a\u201c\u975e\u6b63\u5e38\u7ed3\u675f\u201d( &#8220;Abnormal termination&#8221;)\u7684\u51fa\u9519\u4fe1\u606f\u3002\u5b83\u7684\u683c\u5f0f\u662f\uff1a<code>void terminate();<\/code><\/p>\n\n\n\n<h3>\u6807\u51c6\u4f8b\u5916 (Standard exceptions)<\/h3>\n\n\n\n<p>\u4e00\u4e9bC++ \u6807\u51c6\u8bed\u8a00\u5e93\u4e2d\u7684\u51fd\u6570\u4e5f\u4f1a\u6254\u51fa\u4e00\u4e9b\u5217\u5916\uff0c\u6211\u4eec\u53ef\u4ee5\u7528try \u8bed\u53e5\u6765\u6355\u83b7\u5b83\u4eec\u3002\u8fd9\u4e9b\u4f8b\u5916\u6254\u51fa\u7684\u53c2\u6570\u90fd\u662fstd::exception \u5f15\u7533\u51fa\u7684\u5b50\u7c7b\u7c7b\u578b\u7684\u3002\u8fd9\u4e2a\u7c7b(std::exception) \u88ab\u5b9a\u4e49\u5728C++ \u6807\u51c6\u5934\u6587\u4ef6&nbsp;\u4e2d\uff0c\u7528\u6765\u4f5c\u4e3aexceptions\u6807\u51c6\u7ed3\u6784\u7684\u6a21\u578b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/img5-3-1.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a\u7c7b\u7ed3\u6784\uff0c\u5982\u679c\u4f60\u5305\u62ec\u4e86\u4e00\u4e2acatch \u8bed\u53e5\u5757\u4f7f\u7528\u5730\u5740(reference)\u6765\u6355\u83b7\u8fd9\u4e2a\u7ed3\u6784\u4e2d\u7684\u4efb\u610f\u4e00\u79cd\u5217\u5916 (\u4e5f\u5c31\u662f\u8bf4\u5728\u7c7b\u578b\u540e\u9762\u52a0\u5730\u5740\u7b26 &amp;)\uff0c\u4f60\u540c\u65f6\u53ef\u4ee5\u6355\u83b7\u6240\u6709\u5f15\u7533\u7c7b\u7684\u4f8b\u5916 (C++\u7684\u7ee7\u627f\u539f\u5219)\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e00\u4e2a\u7c7b\u578b\u4e3a bad_typeid \u7684\u4f8b\u5916(exception\u7684\u5f15\u7533\u7c7b)\uff0c\u5728\u8981\u6c42\u7c7b\u578b\u4fe1\u606f\u7684\u5bf9\u8c61\u4e3a\u4e00\u4e2a\u7a7a\u6307\u9488\u7684\u65f6\u5019\u88ab\u6355\u83b7\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ standard exceptions #include &lt;iostream.h&gt; #include &lt;exception&gt; #include &lt;typeinfo&gt; class A {virtual void f() {}; }; int main () { try { A * a = NULL; typeid (*a); } catch (std::exception&amp; e) { cout &lt;&lt; &#8220;Exception: &#8221; &lt;&lt; e.what(); } return 0; }<\/td><td>Exception: Attempted typeid of NULL pointer<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u7528\u8fd9\u4e2a\u6807\u51c6\u7684\u4f8b\u5916\u5c42\u6b21\u7ed3\u6784\u6765\u5b9a\u4e49\u7684\u4f60\u7684\u4f8b\u5916\u6216\u4ece\u5b83\u4eec\u5f15\u7533\u51fa\u65b0\u7684\u4f8b\u5916\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5.3 \u51fa\u9519\u5904\u7406 (Exception handling) \u672c\u8282\u4ecb\u7ecd\u7684\u51fa\u9519\u5904\u7406\u662fANSI-C++ \u6807\u51c6\u5f15\u5165\u7684\u65b0\u529f\u80fd\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684C++ \u7f16\u8bd1\u5668\u4e0d\u517c\u5bb9\u8fd9\u4e2a\u6807\u51c6\uff0c\u5219\u4f60\u53ef\u80fd\u65e0\u6cd5\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u3002 \u5728\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\uff0c\u5f88\u591a\u65f6\u5019\u6211\u4eec\u662f\u65e0\u6cd5\u786e\u5b9a\u4e00\u6bb5\u4ee3\u7801\u662f\u5426\u603b\u662f\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u7684\uff0c\u6216\u8005\u56e0\u4e3a\u7a0b\u5e8f\u8bbf\u95ee\u4e86\u5e76\u4e0d\u5b58\u5728\u7684\u8d44\u6e90\uff0c\u6216\u8005\u7531\u4e8e\u4e00\u4e9b\u53d8\u91cf\u8d85\u51fa\u4e86\u9884\u671f\u7684\u8303\u56f4\uff0c\u7b49\u7b49\u3002 \u8fd9\u4e9b\u60c5\u51b5\u6211\u4eec\u7edf\u79f0\u4e3a\u51fa\u9519\uff08\u4f8b\u5916\uff09\uff0cC++ \u65b0\u8fd1\u5f15\u5165\u7684\u4e09\u79cd\u64cd\u4f5c\u7b26\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u8fd9\u4e9b\u51fa\u9519\u60c5\u51b5\uff1a try, throw \u548c catch \u3002 \u5b83\u4eec\u7684\u4e00\u822c\u7528\u6cd5\u662f\uff1a try { \/\/ code to be tried throw exception; } catch (type exception) { \/\/ code to be executed in case of exception } \u5b83\u4eec\u6240\u8fdb\u884c\u7684\u64cd\u4f5c\u662f\uff1a try \u8bed\u53e5\u5757\u4e2d\u7684\u4ee3\u7801\u88ab\u6b63\u5e38\u6267\u884c\u3002\u5982\u679c\u6709\u4f8b\u5916\u53d1\u751f\uff0c\u4ee3\u7801\u5fc5\u987b\u4f7f\u7528\u5173\u952e\u5b57throw \u548c\u4e00\u4e2a\u53c2\u6570\u6765\u6254\u51fa\u4e00\u4e2a\u4f8b\u5916\u3002\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u4efb\u4f55\u6709\u6548\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u7684\u7c7b\u578b\u53cd\u6620\u4e86\u4f8b\u5916\u7684\u7279\u5f81\u3002 \u5982\u679c\u6709\u4f8b\u5916\u53d1\u751f\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728try \u8bed\u53e5\u5757\u4e2d\u6709\u4e00\u4e2athrow \u6307\u4ee4\u88ab\u6267\u884c\u4e86\uff0c\u5219catch \u8bed\u53e5\u5757\u4f1a\u88ab\u6267\u884c\uff0c\u7528\u6765\u63a5\u6536throw\u4f20\u6765\u7684\u4f8b\u5916\u53c2\u6570\u3002 \u4f8b\u5982\uff1a \/\/ exceptions #include &lt;iostream.h&gt; int main [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":361,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/369"}],"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=369"}],"version-history":[{"count":1,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/369\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/369\/revisions\/370"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/361"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}