{"id":120650,"date":"2026-10-31T23:48:32","date_gmt":"2026-10-31T15:48:32","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/120650.html"},"modified":"2026-10-31T23:48:32","modified_gmt":"2026-10-31T15:48:32","slug":"c-%e5%bc%82%e5%b8%b8%e6%9c%ba%e5%88%b6%e8%af%a6%e8%a7%a3%ef%bc%9a%e4%bb%8e%e6%8a%9b%e5%87%ba%e6%8d%95%e8%8e%b7%e5%88%b0%e5%bc%82%e5%b8%b8%e5%ae%89%e5%85%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/120650.html","title":{"rendered":"C++ \u5f02\u5e38\u673a\u5236\u8be6\u89e3\uff1a\u4ece\u629b\u51fa\u6355\u83b7\u5230\u5f02\u5e38\u5b89\u5168"},"content":{"rendered":"<p>\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u68b3\u7406 C&#043;&#043; \u5f02\u5e38\u5904\u7406\u673a\u5236&#xff0c;\u4ece\u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5\u4e0e C \u8bed\u8a00\u9519\u8bef\u7801\u7684\u5bf9\u6bd4\u51fa\u53d1&#xff0c;\u4f9d\u6b21\u8bb2\u89e3\u5f02\u5e38\u7684\u629b\u51fa\u4e0e\u6355\u83b7\u3001\u6808\u5c55\u5f00\u3001\u5339\u914d\u89c4\u5219\u3001\u5f02\u5e38\u91cd\u65b0\u629b\u51fa\u3001\u5f02\u5e38\u5b89\u5168\u95ee\u9898\u4ee5\u53ca\u5f02\u5e38\u89c4\u8303&#xff0c;\u5e76\u7ed3\u5408\u6807\u51c6\u5e93\u5f02\u5e38\u4f53\u7cfb\u7ed9\u51fa\u5b8c\u6574\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b&#xff0c;\u5e2e\u52a9\u8bfb\u8005\u5efa\u7acb\u5bf9 C&#043;&#043; \u5f02\u5e38\u5904\u7406\u7684\u6574\u4f53\u8ba4\u77e5\u3002<\/p>\n<h3>1. \u5f02\u5e38\u7684\u6982\u5ff5<\/h3>\n<p>\u5f02\u5e38\u5904\u7406\u673a\u5236\u5141\u8bb8\u7a0b\u5e8f\u4e2d\u72ec\u7acb\u5f00\u53d1\u7684\u90e8\u5206&#xff0c;\u5728\u8fd0\u884c\u65f6\u5bf9\u51fa\u73b0\u7684\u95ee\u9898\u8fdb\u884c\u901a\u4fe1\u5e76\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002\u5f02\u5e38\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u5c06\u95ee\u9898\u7684\u68c0\u6d4b\u4e0e\u95ee\u9898\u7684\u89e3\u51b3\u8fc7\u7a0b\u5206\u5f00&#xff1a;\u7a0b\u5e8f\u7684\u4e00\u90e8\u5206\u8d1f\u8d23\u68c0\u6d4b\u95ee\u9898\u7684\u51fa\u73b0&#xff0c;\u7136\u540e\u628a\u89e3\u51b3\u95ee\u9898\u7684\u4efb\u52a1\u4f20\u9012\u7ed9\u7a0b\u5e8f\u7684\u53e6\u4e00\u90e8\u5206&#xff0c;\u68c0\u6d4b\u73af\u8282\u65e0\u987b\u77e5\u9053\u95ee\u9898\u5904\u7406\u6a21\u5757\u7684\u6240\u6709\u7ec6\u8282\u3002<\/p>\n<p>C \u8bed\u8a00\u4e3b\u8981\u901a\u8fc7\u9519\u8bef\u7801\u7684\u5f62\u5f0f\u5904\u7406\u9519\u8bef\u3002\u9519\u8bef\u7801\u672c\u8d28\u5c31\u662f\u5bf9\u9519\u8bef\u4fe1\u606f\u8fdb\u884c\u5206\u7c7b\u7f16\u53f7&#xff0c;\u62ff\u5230\u9519\u8bef\u7801\u4ee5\u540e\u8fd8\u8981\u53bb\u67e5\u8be2\u9519\u8bef\u4fe1\u606f&#xff0c;\u6bd4\u8f83\u9ebb\u70e6\u3002\u800c\u5f02\u5e38\u662f\u629b\u51fa\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u8fd9\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u643a\u5e26\u66f4\u5168\u9762\u7684\u5404\u79cd\u4fe1\u606f\u3002<\/p>\n<h3>2. \u5f02\u5e38\u7684\u629b\u51fa\u548c\u6355\u83b7<\/h3>\n<p>\u7a0b\u5e8f\u51fa\u73b0\u95ee\u9898\u65f6&#xff0c;\u6211\u4eec\u901a\u8fc7\u629b\u51fa&#xff08;throw&#xff09;\u4e00\u4e2a\u5bf9\u8c61\u6765\u5f15\u53d1\u4e00\u4e2a\u5f02\u5e38&#xff0c;\u8be5\u5bf9\u8c61\u7684\u7c7b\u578b\u4ee5\u53ca\u5f53\u524d\u7684\u8c03\u7528\u94fe\u51b3\u5b9a\u4e86\u5e94\u8be5\u7531\u54ea\u4e2a catch \u7684\u5904\u7406\u4ee3\u7801\u6765\u5904\u7406\u8be5\u5f02\u5e38\u3002<\/p>\n<ul>\n<li>\u88ab\u9009\u4e2d\u7684\u5904\u7406\u4ee3\u7801\u662f\u8c03\u7528\u94fe\u4e2d\u4e0e\u8be5\u5bf9\u8c61\u7c7b\u578b\u5339\u914d\u3001\u4e14\u79bb\u629b\u51fa\u5f02\u5e38\u4f4d\u7f6e\u6700\u8fd1\u7684\u90a3\u4e00\u4e2a\u3002<\/li>\n<li>\u6839\u636e\u629b\u51fa\u5bf9\u8c61\u7684\u7c7b\u578b\u548c\u5185\u5bb9&#xff0c;\u7a0b\u5e8f\u7684\u629b\u51fa\u5f02\u5e38\u90e8\u5206\u544a\u77e5\u5f02\u5e38\u5904\u7406\u90e8\u5206\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48\u9519\u8bef\u3002<\/li>\n<li>\u5f53 throw \u6267\u884c\u65f6&#xff0c;throw \u540e\u9762\u7684\u8bed\u53e5\u5c06\u4e0d\u518d\u88ab\u6267\u884c&#xff0c;\u7a0b\u5e8f\u7684\u6267\u884c\u4ece throw \u4f4d\u7f6e\u8df3\u5230\u4e0e\u4e4b\u5339\u914d\u7684 catch \u6a21\u5757\u3002<\/li>\n<li>catch \u53ef\u80fd\u662f\u540c\u4e00\u51fd\u6570\u4e2d\u7684\u4e00\u4e2a\u5c40\u90e8 catch&#xff0c;\u4e5f\u53ef\u80fd\u662f\u8c03\u7528\u94fe\u4e2d\u53e6\u4e00\u4e2a\u51fd\u6570\u4e2d\u7684 catch&#xff0c;\u63a7\u5236\u6743\u4ece throw \u4f4d\u7f6e\u8f6c\u79fb\u5230\u4e86 catch \u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u8fd8\u6709\u4e24\u4e2a\u91cd\u8981\u7684\u542b\u4e49&#xff1a;<\/p>\n<li>\u6cbf\u7740\u8c03\u7528\u94fe\u7684\u51fd\u6570\u53ef\u80fd\u63d0\u65e9\u9000\u51fa\u3002<\/li>\n<li>\u4e00\u65e6\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f&#xff0c;\u6cbf\u7740\u8c03\u7528\u94fe\u521b\u5efa\u7684\u5bf9\u8c61\u90fd\u5c06\u9500\u6bc1\u3002<\/li>\n<p>\u629b\u51fa\u5f02\u5e38\u5bf9\u8c61\u540e&#xff0c;\u4f1a\u751f\u6210\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\u7684\u62f7\u8d1d\u3002\u56e0\u4e3a\u629b\u51fa\u7684\u5f02\u5e38\u5bf9\u8c61\u53ef\u80fd\u662f\u4e00\u4e2a\u5c40\u90e8\u5bf9\u8c61&#xff0c;\u6240\u4ee5\u4f1a\u751f\u6210\u4e00\u4e2a\u62f7\u8d1d\u5bf9\u8c61&#xff0c;\u8fd9\u4e2a\u62f7\u8d1d\u7684\u5bf9\u8c61\u4f1a\u5728 catch \u5b50\u53e5\u540e\u9500\u6bc1&#xff08;\u8fd9\u91cc\u7684\u5904\u7406\u7c7b\u4f3c\u4e8e\u51fd\u6570\u7684\u4f20\u503c\u8fd4\u56de&#xff09;\u3002<\/p>\n<h3>3. \u6808\u5c55\u5f00<\/h3>\n<p>\u629b\u51fa\u5f02\u5e38\u540e&#xff0c;\u7a0b\u5e8f\u6682\u505c\u5f53\u524d\u51fd\u6570\u7684\u6267\u884c&#xff0c;\u5f00\u59cb\u5bfb\u627e\u4e0e\u4e4b\u5339\u914d\u7684 catch \u5b50\u53e5&#xff1a;<\/p>\n<ul>\n<li>\u9996\u5148\u68c0\u67e5 throw \u672c\u8eab\u662f\u5426\u5728 try \u5757\u5185\u90e8&#xff0c;\u5982\u679c\u5728\u5219\u67e5\u627e\u5339\u914d\u7684 catch \u8bed\u53e5&#xff0c;\u5982\u679c\u6709\u5339\u914d\u7684&#xff0c;\u5219\u8df3\u5230 catch \u7684\u5730\u65b9\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<li>\u5982\u679c\u5f53\u524d\u51fd\u6570\u4e2d\u6ca1\u6709 try\/catch \u5b50\u53e5&#xff0c;\u6216\u8005\u6709 try\/catch \u5b50\u53e5\u4f46\u662f\u7c7b\u578b\u4e0d\u5339\u914d&#xff0c;\u5219\u9000\u51fa\u5f53\u524d\u51fd\u6570&#xff0c;\u7ee7\u7eed\u5728\u5916\u5c42\u8c03\u7528\u51fd\u6570\u94fe\u4e2d\u67e5\u627e\u3002\u4e0a\u8ff0\u67e5\u627e catch \u7684\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u6808\u5c55\u5f00\u3002<\/li>\n<li>\u5982\u679c\u5230\u8fbe main \u51fd\u6570&#xff0c;\u4f9d\u65e7\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684 catch \u5b50\u53e5&#xff0c;\u7a0b\u5e8f\u4f1a\u8c03\u7528\u6807\u51c6\u5e93\u7684 terminate \u51fd\u6570\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/li>\n<li>\u5982\u679c\u627e\u5230\u5339\u914d\u7684 catch \u5b50\u53e5&#xff0c;\u5904\u7406\u540e catch \u5b50\u53e5\u4ee3\u7801\u4f1a\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u9664\u96f6\u5f02\u5e38\u7684\u4f8b\u5b50&#xff0c;\u89c2\u5bdf\u5f02\u5e38\u5728\u4e0d\u540c\u8c03\u7528\u5c42\u7ea7\u4e4b\u95f4\u5982\u4f55\u4f20\u9012&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<\/p>\n<p>double Divide(int a, int b)<br \/>\n{<br \/>\n    try<br \/>\n    {<br \/>\n        \/\/ \u5f53 b &#061;&#061; 0 \u65f6\u629b\u51fa\u5f02\u5e38<br \/>\n        if (b &#061;&#061; 0)<br \/>\n        {<br \/>\n            string s(&#034;Divide by zero condition!&#034;);<br \/>\n            throw s;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            return ((double)a \/ (double)b);<br \/>\n        }<br \/>\n    }<br \/>\n    catch (int errid)<br \/>\n    {<br \/>\n        cout &lt;&lt; errid &lt;&lt; endl;<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>void Func()<br \/>\n{<br \/>\n    int len, time;<br \/>\n    cin &gt;&gt; len &gt;&gt; time;<br \/>\n    try<br \/>\n    {<br \/>\n        cout &lt;&lt; Divide(len, time) &lt;&lt; endl;<br \/>\n    }<br \/>\n    catch (const char* errmsg)<br \/>\n    {<br \/>\n        cout &lt;&lt; errmsg &lt;&lt; endl;<br \/>\n    }<br \/>\n    cout &lt;&lt; __FUNCTION__ &lt;&lt; &#034;:&#034; &lt;&lt; __LINE__ &lt;&lt; &#034;\u884c\u6267\u884c&#034; &lt;&lt; endl;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    while (1)<br \/>\n    {<br \/>\n        try<br \/>\n        {<br \/>\n            Func();<br \/>\n        }<br \/>\n        catch (const string&amp; errmsg)<br \/>\n        {<br \/>\n            cout &lt;&lt; errmsg &lt;&lt; endl;<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u5728 Divide \u4e2d\u629b\u51fa\u7684\u662f string \u7c7b\u578b\u5bf9\u8c61&#xff0c;\u672c\u5c42\u7684 catch(int) \u4e0e Func \u4e2d\u7684 catch(const char*) \u90fd\u65e0\u6cd5\u5339\u914d&#xff0c;\u6700\u7ec8\u7531 main \u4e2d\u7684 catch(const string&amp;) \u6355\u83b7&#xff0c;\u8fd9\u6b63\u662f\u6808\u5c55\u5f00\u7684\u5178\u578b\u8fc7\u7a0b\u3002<\/p>\n<h3>4. \u67e5\u627e\u5339\u914d\u7684\u5904\u7406\u4ee3\u7801<\/h3>\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b&#xff0c;\u629b\u51fa\u5bf9\u8c61\u548c catch \u662f\u7c7b\u578b\u5b8c\u5168\u5339\u914d\u7684&#xff1b;\u5982\u679c\u6709\u591a\u4e2a\u7c7b\u578b\u5339\u914d\u7684&#xff0c;\u5c31\u9009\u62e9\u79bb\u5b83\u4f4d\u7f6e\u66f4\u8fd1\u7684\u90a3\u4e2a\u3002\u4f46\u4e5f\u6709\u4e00\u4e9b\u4f8b\u5916&#xff0c;\u5141\u8bb8\u4ee5\u4e0b\u8f6c\u6362&#xff1a;<\/p>\n<ul>\n<li>\u4ece\u975e\u5e38\u91cf\u5411\u5e38\u91cf\u7684\u7c7b\u578b\u8f6c\u6362&#xff0c;\u4e5f\u5c31\u662f\u6743\u9650\u7f29\u5c0f\u3002<\/li>\n<li>\u6570\u7ec4\u8f6c\u6362\u6210\u6307\u5411\u6570\u7ec4\u5143\u7d20\u7c7b\u578b\u7684\u6307\u9488&#xff0c;\u51fd\u6570\u88ab\u8f6c\u6362\u6210\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\u3002<\/li>\n<li>\u4ece\u6d3e\u751f\u7c7b\u5411\u57fa\u7c7b\u7c7b\u578b\u7684\u8f6c\u6362&#xff0c;\u8fd9\u4e2a\u70b9\u975e\u5e38\u5b9e\u7528&#xff0c;\u5b9e\u9645\u4e2d\u7ee7\u627f\u4f53\u7cfb\u57fa\u672c\u90fd\u662f\u7528\u8fd9\u79cd\u65b9\u5f0f\u8bbe\u8ba1\u7684\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u5230 main \u51fd\u6570&#xff0c;\u5f02\u5e38\u4ecd\u65e7\u6ca1\u6709\u88ab\u5339\u914d\u5c31\u4f1a\u7ec8\u6b62\u7a0b\u5e8f\u3002\u4e0d\u662f\u53d1\u751f\u4e25\u91cd\u9519\u8bef\u7684\u60c5\u51b5\u4e0b&#xff0c;\u6211\u4eec\u662f\u4e0d\u671f\u671b\u7a0b\u5e8f\u7ec8\u6b62\u7684&#xff0c;\u6240\u4ee5\u4e00\u822c main \u51fd\u6570\u4e2d\u6700\u540e\u90fd\u4f1a\u4f7f\u7528 catch(&#8230;)&#xff0c;\u5b83\u53ef\u4ee5\u6355\u83b7\u4efb\u610f\u7c7b\u578b\u7684\u5f02\u5e38&#xff0c;\u4f46\u662f\u4e0d\u77e5\u9053\u5f02\u5e38\u9519\u8bef\u662f\u4ec0\u4e48\u3002<\/p>\n<p>\u4e0b\u9762\u6a21\u62df\u4e00\u4e2a\u670d\u52a1\u7684\u51e0\u4e2a\u6a21\u5757&#xff0c;\u6bcf\u4e2a\u6a21\u5757\u7684\u5f02\u5e38\u7c7b\u90fd\u7ee7\u627f\u81ea Exception&#xff0c;\u6700\u540e\u7edf\u4e00\u6355\u83b7\u57fa\u7c7b&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;chrono&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u4e00\u822c\u5927\u578b\u9879\u76ee\u7a0b\u5e8f\u624d\u4f1a\u4f7f\u7528\u5f02\u5e38&#xff0c;\u4e0b\u9762\u6211\u4eec\u6a21\u62df\u8bbe\u8ba1\u4e00\u4e2a\u670d\u52a1\u7684\u51e0\u4e2a\u6a21\u5757<br \/>\n\/\/ \u6bcf\u4e2a\u6a21\u5757\u7684\u7ee7\u627f\u90fd\u662f Exception \u7684\u6d3e\u751f\u7c7b&#xff0c;\u6bcf\u4e2a\u6a21\u5757\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5df1\u7684\u6570\u636e<br \/>\n\/\/ \u6700\u540e\u6355\u83b7\u65f6&#xff0c;\u6211\u4eec\u6355\u83b7\u57fa\u7c7b\u5c31\u53ef\u4ee5<br \/>\nclass Exception<br \/>\n{<br \/>\npublic:<br \/>\n    Exception(const string&amp; errmsg, int id)<br \/>\n        : _errmsg(errmsg)<br \/>\n        , _id(id)<br \/>\n    {}<\/p>\n<p>    virtual string what() const<br \/>\n    {<br \/>\n        return _errmsg;<br \/>\n    }<\/p>\n<p>    int getid() const<br \/>\n    {<br \/>\n        return _id;<br \/>\n    }<\/p>\n<p>protected:<br \/>\n    string _errmsg;<br \/>\n    int _id;<br \/>\n};<\/p>\n<p>class SqlException : public Exception<br \/>\n{<br \/>\npublic:<br \/>\n    SqlException(const string&amp; errmsg, int id, const string&amp; sql)<br \/>\n        : Exception(errmsg, id)<br \/>\n        , _sql(sql)<br \/>\n    {}<\/p>\n<p>    virtual string what() const<br \/>\n    {<br \/>\n        string str &#061; &#034;SqlException:&#034;;<br \/>\n        str &#043;&#061; _errmsg;<br \/>\n        str &#043;&#061; &#034;-&gt;&#034;;<br \/>\n        str &#043;&#061; _sql;<br \/>\n        return str;<br \/>\n    }<\/p>\n<p>private:<br \/>\n    const string _sql;<br \/>\n};<\/p>\n<p>class CacheException : public Exception<br \/>\n{<br \/>\npublic:<br \/>\n    CacheException(const string&amp; errmsg, int id)<br \/>\n        : Exception(errmsg, id)<br \/>\n    {}<\/p>\n<p>    virtual string what() const<br \/>\n    {<br \/>\n        string str &#061; &#034;CacheException:&#034;;<br \/>\n        str &#043;&#061; _errmsg;<br \/>\n        return str;<br \/>\n    }<br \/>\n};<\/p>\n<p>class HttpException : public Exception<br \/>\n{<br \/>\npublic:<br \/>\n    HttpException(const string&amp; errmsg, int id, const string&amp; type)<br \/>\n        : Exception(errmsg, id)<br \/>\n        , _type(type)<br \/>\n    {}<\/p>\n<p>    virtual string what() const<br \/>\n    {<br \/>\n        string str &#061; &#034;HttpException:&#034;;<br \/>\n        str &#043;&#061; _type;<br \/>\n        str &#043;&#061; &#034;:&#034;;<br \/>\n        str &#043;&#061; _errmsg;<br \/>\n        return str;<br \/>\n    }<\/p>\n<p>private:<br \/>\n    const string _type;<br \/>\n};<\/p>\n<p>void SQLMgr()<br \/>\n{<br \/>\n    if (rand() % 7 &#061;&#061; 0)<br \/>\n    {<br \/>\n        throw SqlException(&#034;\u6743\u9650\u4e0d\u8db3&#034;, 100, &#034;select * from name &#061; &#039;\u5f20\u4e09&#039;&#034;);<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        cout &lt;&lt; &#034;SQLMgr \u8c03\u7528\u6210\u529f&#034; &lt;&lt; endl;<br \/>\n    }<br \/>\n}<\/p>\n<p>void CacheMgr()<br \/>\n{<br \/>\n    if (rand() % 5 &#061;&#061; 0)<br \/>\n    {<br \/>\n        throw CacheException(&#034;\u6743\u9650\u4e0d\u8db3&#034;, 100);<br \/>\n    }<br \/>\n    else if (rand() % 6 &#061;&#061; 0)<br \/>\n    {<br \/>\n        throw CacheException(&#034;\u6570\u636e\u4e0d\u5b58\u5728&#034;, 101);<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        cout &lt;&lt; &#034;CacheMgr \u8c03\u7528\u6210\u529f&#034; &lt;&lt; endl;<br \/>\n    }<br \/>\n    SQLMgr();<br \/>\n}<\/p>\n<p>void HttpServer()<br \/>\n{<br \/>\n    if (rand() % 3 &#061;&#061; 0)<br \/>\n    {<br \/>\n        throw HttpException(&#034;\u8bf7\u6c42\u8d44\u6e90\u4e0d\u5b58\u5728&#034;, 100, &#034;get&#034;);<br \/>\n    }<br \/>\n    else if (rand() % 4 &#061;&#061; 0)<br \/>\n    {<br \/>\n        throw HttpException(&#034;\u6743\u9650\u4e0d\u8db3&#034;, 101, &#034;post&#034;);<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        cout &lt;&lt; &#034;HttpServer \u8c03\u7528\u6210\u529f&#034; &lt;&lt; endl;<br \/>\n    }<br \/>\n    CacheMgr();<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    srand(time(0));<br \/>\n    while (1)<br \/>\n    {<br \/>\n        this_thread::sleep_for(chrono::seconds(1));<br \/>\n        try<br \/>\n        {<br \/>\n            HttpServer();<br \/>\n        }<br \/>\n        catch (const Exception&amp; e) \/\/ \u8fd9\u91cc\u6355\u83b7\u57fa\u7c7b&#xff0c;\u57fa\u7c7b\u5bf9\u8c61\u548c\u6d3e\u751f\u7c7b\u5bf9\u8c61\u90fd\u53ef\u4ee5\u88ab\u6355\u83b7<br \/>\n        {<br \/>\n            cout &lt;&lt; e.what() &lt;&lt; endl;<br \/>\n        }<br \/>\n        catch (&#8230;)<br \/>\n        {<br \/>\n            cout &lt;&lt; &#034;Unkown Exception&#034; &lt;&lt; endl;<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n} <\/p>\n<h3>5. \u5f02\u5e38\u91cd\u65b0\u629b\u51fa<\/h3>\n<p>\u6709\u65f6 catch \u5230\u4e00\u4e2a\u5f02\u5e38\u5bf9\u8c61\u540e&#xff0c;\u9700\u8981\u5bf9\u9519\u8bef\u8fdb\u884c\u5206\u7c7b&#xff1a;\u5176\u4e2d\u7684\u67d0\u79cd\u5f02\u5e38\u9519\u8bef\u9700\u8981\u8fdb\u884c\u7279\u6b8a\u7684\u5904\u7406&#xff0c;\u5176\u4ed6\u9519\u8bef\u5219\u91cd\u65b0\u629b\u51fa\u5f02\u5e38\u7ed9\u5916\u5c42\u8c03\u7528\u94fe\u5904\u7406\u3002\u6355\u83b7\u5f02\u5e38\u540e\u9700\u8981\u91cd\u65b0\u629b\u51fa&#xff0c;\u76f4\u63a5 throw; \u5c31\u53ef\u4ee5\u628a\u6355\u83b7\u7684\u5bf9\u8c61\u76f4\u63a5\u629b\u51fa\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u7a0b\u5e8f\u6a21\u62df\u4e86\u804a\u5929\u65f6\u53d1\u9001\u6d88\u606f\u7684\u573a\u666f&#xff1a;\u53d1\u9001\u5931\u8d25\u4f1a\u6355\u83b7\u5f02\u5e38&#xff0c;\u4f46\u53ef\u80fd\u5728\u7535\u68af\u3001\u5730\u4e0b\u5ba4\u7b49\u573a\u666f\u624b\u673a\u4fe1\u53f7\u4e0d\u597d&#xff0c;\u5219\u9700\u8981\u591a\u6b21\u5c1d\u8bd5&#xff1b;\u5982\u679c\u591a\u6b21\u5c1d\u8bd5\u90fd\u53d1\u9001\u4e0d\u51fa\u53bb&#xff0c;\u5c31\u9700\u8981\u6355\u83b7\u5f02\u5e38\u518d\u91cd\u65b0\u629b\u51fa\u3002\u5176\u6b21\u5982\u679c\u4e0d\u662f\u7f51\u7edc\u5dee\u5bfc\u81f4\u7684\u9519\u8bef&#xff0c;\u6355\u83b7\u540e\u4e5f\u8981\u91cd\u65b0\u629b\u51fa\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\nusing namespace std;<\/p>\n<p>void _SendMsg(const string&amp; s)<br \/>\n{<br \/>\n    if (rand() % 2 &#061;&#061; 0)<br \/>\n    {<br \/>\n        throw HttpException(&#034;\u7f51\u7edc\u4e0d\u7a33\u5b9a&#xff0c;\u53d1\u9001\u5931\u8d25&#034;, 102, &#034;put&#034;);<br \/>\n    }<br \/>\n    else if (rand() % 7 &#061;&#061; 0)<br \/>\n    {<br \/>\n        throw HttpException(&#034;\u4f60\u5df2\u7ecf\u4e0d\u662f\u5bf9\u8c61\u7684\u597d\u53cb&#xff0c;\u53d1\u9001\u5931\u8d25&#034;, 103, &#034;put&#034;);<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        cout &lt;&lt; &#034;\u53d1\u9001\u6210\u529f&#034; &lt;&lt; endl;<br \/>\n    }<br \/>\n}<\/p>\n<p>void SendMsg(const string&amp; s)<br \/>\n{<br \/>\n    \/\/ \u53d1\u9001\u6d88\u606f\u5931\u8d25&#xff0c;\u5219\u518d\u91cd\u8bd5 3 \u6b21<br \/>\n    for (size_t i &#061; 0; i &lt; 4; i&#043;&#043;)<br \/>\n    {<br \/>\n        try<br \/>\n        {<br \/>\n            _SendMsg(s);<br \/>\n            break;<br \/>\n        }<br \/>\n        catch (const Exception&amp; e)<br \/>\n        {<br \/>\n            \/\/ \u6355\u83b7\u5f02\u5e38&#xff0c;if \u4e2d\u662f 102 \u53f7\u9519\u8bef&#xff0c;\u7f51\u7edc\u4e0d\u7a33\u5b9a&#xff0c;\u5219\u91cd\u65b0\u53d1\u9001<br \/>\n            \/\/ \u6355\u83b7\u5f02\u5e38&#xff0c;else \u4e2d\u4e0d\u662f 102 \u53f7\u9519\u8bef&#xff0c;\u5219\u5c06\u5f02\u5e38\u91cd\u65b0\u629b\u51fa<br \/>\n            if (e.getid() &#061;&#061; 102)<br \/>\n            {<br \/>\n                \/\/ \u91cd\u8bd5\u4e09\u6b21\u4ee5\u540e\u8fd8\u5931\u8d25\u4e86&#xff0c;\u5219\u8bf4\u660e\u7f51\u7edc\u592a\u5dee\u4e86&#xff0c;\u91cd\u65b0\u629b\u51fa\u5f02\u5e38<br \/>\n                if (i &#061;&#061; 3)<br \/>\n                    throw;<br \/>\n                cout &lt;&lt; &#034;\u5f00\u59cb\u7b2c&#034; &lt;&lt; i &#043; 1 &lt;&lt; &#034;\u6b21\u91cd\u8bd5&#034; &lt;&lt; endl;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                throw;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    srand(time(0));<br \/>\n    string str;<br \/>\n    while (cin &gt;&gt; str)<br \/>\n    {<br \/>\n        try<br \/>\n        {<br \/>\n            SendMsg(str);<br \/>\n        }<br \/>\n        catch (const Exception&amp; e)<br \/>\n        {<br \/>\n            cout &lt;&lt; e.what() &lt;&lt; endl &lt;&lt; endl;<br \/>\n        }<br \/>\n        catch (&#8230;)<br \/>\n        {<br \/>\n            cout &lt;&lt; &#034;Unkown Exception&#034; &lt;&lt; endl;<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n} <\/p>\n<h3>6. \u5f02\u5e38\u5b89\u5168\u95ee\u9898<\/h3>\n<p>\u5f02\u5e38\u629b\u51fa\u540e&#xff0c;\u540e\u9762\u7684\u4ee3\u7801\u5c31\u4e0d\u518d\u6267\u884c\u3002\u5982\u679c\u524d\u9762\u7533\u8bf7\u4e86\u8d44\u6e90&#xff08;\u5185\u5b58\u3001\u9501\u7b49&#xff09;&#xff0c;\u540e\u9762\u8fdb\u884c\u91ca\u653e&#xff0c;\u4f46\u4e2d\u95f4\u53ef\u80fd\u4f1a\u629b\u5f02\u5e38&#xff0c;\u5c31\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6ca1\u6709\u91ca\u653e&#xff0c;\u8fd9\u91cc\u7531\u4e8e\u5f02\u5e38\u5c31\u5f15\u53d1\u4e86\u8d44\u6e90\u6cc4\u6f0f&#xff0c;\u4ea7\u751f\u5b89\u5168\u6027\u7684\u95ee\u9898\u3002\u4e2d\u95f4\u6211\u4eec\u9700\u8981\u6355\u83b7\u5f02\u5e38&#xff0c;\u91ca\u653e\u8d44\u6e90\u540e\u518d\u91cd\u65b0\u629b\u51fa&#xff1b;\u5f53\u7136&#xff0c;\u540e\u9762\u667a\u80fd\u6307\u9488\u7ae0\u8282\u8bb2\u7684 RAII \u65b9\u5f0f\u89e3\u51b3\u8fd9\u79cd\u95ee\u9898\u662f\u66f4\u597d\u7684\u3002<\/p>\n<p>\u5176\u6b21&#xff0c;\u6790\u6784\u51fd\u6570\u4e2d\u5982\u679c\u629b\u51fa\u5f02\u5e38\u4e5f\u8981\u8c28\u614e\u5904\u7406\u3002\u6bd4\u5982\u6790\u6784\u51fd\u6570\u8981\u91ca\u653e 10 \u4e2a\u8d44\u6e90&#xff0c;\u91ca\u653e\u5230\u7b2c 5 \u4e2a\u65f6\u629b\u51fa\u5f02\u5e38&#xff0c;\u5219\u4e5f\u9700\u8981\u6355\u83b7\u5904\u7406&#xff0c;\u5426\u5219\u540e\u9762\u7684 5 \u4e2a\u8d44\u6e90\u5c31\u6ca1\u91ca\u653e&#xff0c;\u4e5f\u8d44\u6e90\u6cc4\u6f0f\u4e86\u3002\u300aEffective C&#043;&#043;\u300b\u7b2c 8 \u4e2a\u6761\u6b3e\u4e5f\u4e13\u95e8\u8bb2\u4e86\u8fd9\u4e2a\u95ee\u9898&#xff1a;\u522b\u8ba9\u5f02\u5e38\u9003\u79bb\u6790\u6784\u51fd\u6570\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>double Divide(int a, int b)<br \/>\n{<br \/>\n    \/\/ \u5f53 b &#061;&#061; 0 \u65f6\u629b\u51fa\u5f02\u5e38<br \/>\n    if (b &#061;&#061; 0)<br \/>\n    {<br \/>\n        throw &#034;Division by zero condition!&#034;;<br \/>\n    }<br \/>\n    return (double)a \/ (double)b;<br \/>\n}<\/p>\n<p>void Func()<br \/>\n{<br \/>\n    \/\/ \u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\u5982\u679c\u53d1\u751f\u9664 0 \u9519\u8bef\u629b\u51fa\u5f02\u5e38&#xff0c;\u4e0b\u9762\u7684 array \u6ca1\u6709\u5f97\u5230\u91ca\u653e\u3002<br \/>\n    \/\/ \u6240\u4ee5\u8fd9\u91cc\u6355\u83b7\u5f02\u5e38\u540e\u5e76\u4e0d\u5904\u7406\u5f02\u5e38&#xff0c;\u5f02\u5e38\u8fd8\u662f\u4ea4\u7ed9\u5916\u5c42\u5904\u7406&#xff0c;\u8fd9\u91cc\u6355\u83b7\u4e86\u518d\u91cd\u65b0\u629b\u51fa\u53bb\u3002<br \/>\n    int* array &#061; new int[10];<br \/>\n    try<br \/>\n    {<br \/>\n        int len, time;<br \/>\n        cin &gt;&gt; len &gt;&gt; time;<br \/>\n        cout &lt;&lt; Divide(len, time) &lt;&lt; endl;<br \/>\n    }<br \/>\n    catch (&#8230;)<br \/>\n    {<br \/>\n        \/\/ \u6355\u83b7\u5f02\u5e38\u91ca\u653e\u5185\u5b58<br \/>\n        cout &lt;&lt; &#034;delete []&#034; &lt;&lt; array &lt;&lt; endl;<br \/>\n        delete[] array;<br \/>\n        throw; \/\/ \u5f02\u5e38\u91cd\u65b0\u629b\u51fa&#xff0c;\u6355\u83b7\u5230\u4ec0\u4e48\u629b\u51fa\u4ec0\u4e48<br \/>\n    }<br \/>\n    cout &lt;&lt; &#034;delete []&#034; &lt;&lt; array &lt;&lt; endl;<br \/>\n    delete[] array;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    try<br \/>\n    {<br \/>\n        Func();<br \/>\n    }<br \/>\n    catch (const char* errmsg)<br \/>\n    {<br \/>\n        cout &lt;&lt; errmsg &lt;&lt; endl;<br \/>\n    }<br \/>\n    catch (const exception&amp; e)<br \/>\n    {<br \/>\n        cout &lt;&lt; e.what() &lt;&lt; endl;<br \/>\n    }<br \/>\n    catch (&#8230;)<br \/>\n    {<br \/>\n        cout &lt;&lt; &#034;Unkown Exception&#034; &lt;&lt; endl;<br \/>\n    }<br \/>\n    return 0;<br \/>\n} <\/p>\n<h3>7. \u5f02\u5e38\u89c4\u8303<\/h3>\n<p>\u5bf9\u4e8e\u7528\u6237\u548c\u7f16\u8bd1\u5668\u800c\u8a00&#xff0c;\u9884\u5148\u77e5\u9053\u67d0\u4e2a\u7a0b\u5e8f\u4f1a\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u5927\u6709\u88e8\u76ca&#xff0c;\u77e5\u9053\u67d0\u4e2a\u51fd\u6570\u662f\u5426\u4f1a\u629b\u51fa\u5f02\u5e38\u6709\u52a9\u4e8e\u7b80\u5316\u8c03\u7528\u51fd\u6570\u7684\u4ee3\u7801\u3002<\/p>\n<ul>\n<li>C&#043;&#043;98 \u4e2d\u51fd\u6570\u53c2\u6570\u5217\u8868\u7684\u540e\u9762\u63a5 throw()&#xff0c;\u8868\u793a\u51fd\u6570\u4e0d\u629b\u5f02\u5e38&#xff1b;\u51fd\u6570\u53c2\u6570\u5217\u8868\u7684\u540e\u9762\u63a5 throw(\u7c7b\u578b1, \u7c7b\u578b2&#8230;) \u8868\u793a\u53ef\u80fd\u4f1a\u629b\u51fa\u591a\u79cd\u7c7b\u578b\u7684\u5f02\u5e38&#xff0c;\u53ef\u80fd\u629b\u51fa\u7684\u7c7b\u578b\u7528\u9017\u53f7\u5206\u5272\u3002<\/li>\n<li>C&#043;&#043;98 \u7684\u8fd9\u79cd\u65b9\u5f0f\u8fc7\u4e8e\u590d\u6742&#xff0c;\u5b9e\u8df5\u4e2d\u5e76\u4e0d\u597d\u7528\u3002C&#043;&#043;11 \u4e2d\u8fdb\u884c\u4e86\u7b80\u5316&#xff1a;\u51fd\u6570\u53c2\u6570\u5217\u8868\u540e\u9762\u52a0 noexcept \u8868\u793a\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38&#xff0c;\u4ec0\u4e48\u90fd\u4e0d\u52a0\u8868\u793a\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li>\u7f16\u8bd1\u5668\u5e76\u4e0d\u4f1a\u5728\u7f16\u8bd1\u65f6\u68c0\u67e5 noexcept\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u7528 noexcept \u4fee\u9970\u4e86&#xff0c;\u4f46\u662f\u540c\u65f6\u53c8\u5305\u542b\u4e86 throw \u8bed\u53e5\u6216\u8005\u8c03\u7528\u7684\u51fd\u6570\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38&#xff0c;\u7f16\u8bd1\u5668\u8fd8\u662f\u4f1a\u987a\u5229\u7f16\u8bd1\u901a\u8fc7&#xff08;\u6709\u4e9b\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u62a5\u4e2a\u8b66\u544a&#xff09;\u3002\u4f46\u662f\u4e00\u4e2a\u58f0\u660e\u4e86 noexcept \u7684\u51fd\u6570\u629b\u51fa\u4e86\u5f02\u5e38&#xff0c;\u7a0b\u5e8f\u4f1a\u8c03\u7528 terminate \u7ec8\u6b62\u7a0b\u5e8f\u3002<\/li>\n<li>noexcept(expression) \u8fd8\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u8fd0\u7b97\u7b26\u53bb\u68c0\u6d4b\u4e00\u4e2a\u8868\u8fbe\u5f0f\u662f\u5426\u4f1a\u629b\u51fa\u5f02\u5e38&#xff0c;\u53ef\u80fd\u4f1a\u5219\u8fd4\u56de false&#xff0c;\u4e0d\u4f1a\u5c31\u8fd4\u56de true\u3002<\/li>\n<\/ul>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;new&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ C&#043;&#043;98<br \/>\n\/\/ \u8fd9\u91cc\u8868\u793a\u8fd9\u4e2a\u51fd\u6570\u53ea\u4f1a\u629b\u51fa bad_alloc \u7684\u5f02\u5e38<br \/>\nvoid* operator new (std::size_t size) throw (std::bad_alloc);<br \/>\n\/\/ \u8fd9\u91cc\u8868\u793a\u8fd9\u4e2a\u51fd\u6570\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38<br \/>\nvoid* operator delete (std::size_t size, void* ptr) throw();<\/p>\n<p>\/\/ C&#043;&#043;11<br \/>\nsize_type size() const noexcept;<br \/>\niterator begin() noexcept;<br \/>\nconst_iterator begin() const noexcept;<\/p>\n<p>double Divide(int a, int b) noexcept<br \/>\n{<br \/>\n    \/\/ \u5f53 b &#061;&#061; 0 \u65f6\u629b\u51fa\u5f02\u5e38<br \/>\n    if (b &#061;&#061; 0)<br \/>\n    {<br \/>\n        throw &#034;Division by zero condition!&#034;;<br \/>\n    }<br \/>\n    return (double)a \/ (double)b;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    try<br \/>\n    {<br \/>\n        int len, time;<br \/>\n        cin &gt;&gt; len &gt;&gt; time;<br \/>\n        cout &lt;&lt; Divide(len, time) &lt;&lt; endl;<br \/>\n    }<br \/>\n    catch (const char* errmsg)<br \/>\n    {<br \/>\n        cout &lt;&lt; errmsg &lt;&lt; endl;<br \/>\n    }<br \/>\n    catch (&#8230;)<br \/>\n    {<br \/>\n        cout &lt;&lt; &#034;Unkown Exception&#034; &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    int i &#061; 0;<br \/>\n    cout &lt;&lt; noexcept(Divide(1, 2)) &lt;&lt; endl;<br \/>\n    cout &lt;&lt; noexcept(Divide(1, 0)) &lt;&lt; endl;<br \/>\n    cout &lt;&lt; noexcept(&#043;&#043;i) &lt;&lt; endl;<br \/>\n    return 0;<br \/>\n} <\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;noexcept \u8fd0\u7b97\u7b26\u5224\u65ad\u7684\u662f\u8868\u8fbe\u5f0f\u672c\u8eab\u662f\u5426\u58f0\u660e\u53ef\u80fd\u629b\u51fa\u5f02\u5e38&#xff0c;\u800c\u4e0d\u662f\u8fd0\u884c\u65f6\u662f\u5426\u771f\u7684\u629b\u51fa\u5f02\u5e38&#xff0c;\u56e0\u6b64 noexcept(Divide(1, 0)) \u7684\u7ed3\u679c\u540c\u6837\u4e3a true\u3002<\/p>\n<h3>8. \u6807\u51c6\u5e93\u7684\u5f02\u5e38<\/h3>\n<p>C&#043;&#043; \u6807\u51c6\u5e93\u4e5f\u5b9a\u4e49\u4e86\u4e00\u5957\u81ea\u5df1\u7684\u5f02\u5e38\u7ee7\u627f\u4f53\u7cfb&#xff0c;\u57fa\u7c7b\u662f exception\u3002\u6240\u4ee5\u65e5\u5e38\u5199\u7a0b\u5e8f&#xff0c;\u9700\u8981\u5728\u4e3b\u51fd\u6570\u6355\u83b7 exception \u5373\u53ef&#xff1b;\u8981\u83b7\u53d6\u5f02\u5e38\u4fe1\u606f&#xff0c;\u8c03\u7528 what \u51fd\u6570&#xff0c;what \u662f\u4e00\u4e2a\u865a\u51fd\u6570&#xff0c;\u6d3e\u751f\u7c7b\u53ef\u4ee5\u91cd\u5199\u3002<\/p>\n<p>\u6807\u51c6\u5e93\u5f02\u5e38\u4f53\u7cfb\u53ef\u53c2\u8003\u5b98\u65b9\u6587\u6863&#xff1a;exception &#8211; C&#043;&#043; Reference<\/p>\n<table>\n<tr>\u5f02\u5e38\u7c7b\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>std::exception<\/td>\n<td>\u6240\u6709\u6807\u51c6\u5f02\u5e38\u7684\u57fa\u7c7b&#xff0c;\u63d0\u4f9b\u865a\u51fd\u6570 what()<\/td>\n<\/tr>\n<tr>\n<td>std::bad_alloc<\/td>\n<td>new \u7533\u8bf7\u5185\u5b58\u5931\u8d25\u65f6\u629b\u51fa<\/td>\n<\/tr>\n<tr>\n<td>std::bad_cast<\/td>\n<td>\u52a8\u6001\u7c7b\u578b\u8f6c\u6362 dynamic_cast \u5931\u8d25\u65f6\u629b\u51fa<\/td>\n<\/tr>\n<tr>\n<td>std::out_of_range<\/td>\n<td>\u8bbf\u95ee\u8d8a\u754c&#xff0c;\u5982 vector::at \u4e0b\u6807\u975e\u6cd5<\/td>\n<\/tr>\n<tr>\n<td>std::logic_error<\/td>\n<td>\u903b\u8f91\u9519\u8bef\u7c7b\u5f02\u5e38\u7684\u57fa\u7c7b<\/td>\n<\/tr>\n<tr>\n<td>std::runtime_error<\/td>\n<td>\u8fd0\u884c\u65f6\u9519\u8bef\u7c7b\u5f02\u5e38\u7684\u57fa\u7c7b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>9. \u5c0f\u7ed3<\/h3>\n<ul>\n<li>\u5f02\u5e38\u628a\u9519\u8bef\u68c0\u6d4b\u4e0e\u9519\u8bef\u5904\u7406\u89e3\u8026&#xff0c;\u6bd4\u9519\u8bef\u7801\u80fd\u643a\u5e26\u66f4\u4e30\u5bcc\u7684\u4fe1\u606f\u3002<\/li>\n<li>throw \u540e\u6cbf\u8c03\u7528\u94fe\u8fdb\u884c\u6808\u5c55\u5f00&#xff0c;\u5339\u914d\u6700\u8fd1\u7684\u3001\u7c7b\u578b\u517c\u5bb9\u7684 catch&#xff1b;\u5230 main \u4ecd\u672a\u5339\u914d\u5219\u8c03\u7528 terminate\u3002<\/li>\n<li>\u5229\u7528\u7ee7\u627f\u4f53\u7cfb\u8bbe\u8ba1\u5f02\u5e38\u7c7b&#xff0c;\u5728 main \u4e2d\u6355\u83b7\u57fa\u7c7b\u5f15\u7528&#xff0c;\u662f\u5927\u578b\u9879\u76ee\u7684\u5e38\u89c1\u505a\u6cd5\u3002<\/li>\n<li>throw; \u53ef\u91cd\u65b0\u629b\u51fa\u5f53\u524d\u5f02\u5e38&#xff0c;\u7528\u4e8e\u5206\u5c42\u5904\u7406\u4e0e\u91cd\u8bd5\u903b\u8f91\u3002<\/li>\n<li>\u6ce8\u610f\u5f02\u5e38\u5b89\u5168&#xff1a;\u8d44\u6e90\u91ca\u653e\u8981\u653e\u5728\u6355\u83b7\u8def\u5f84\u4e2d&#xff0c;\u6216\u4f7f\u7528 RAII&#xff1b;\u4e0d\u8981\u8ba9\u5f02\u5e38\u9003\u79bb\u6790\u6784\u51fd\u6570\u3002<\/li>\n<li>C&#043;&#043;11 \u8d77\u7528 noexcept \u58f0\u660e\u4e0d\u629b\u5f02\u5e38&#xff0c;\u5e76\u53ef\u7528 noexcept(expr) \u8fd0\u7b97\u7b26\u68c0\u6d4b\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u68b3\u7406 C \u5f02\u5e38\u5904\u7406\u673a\u5236&#xff0c;\u4ece\u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5\u4e0e C \u8bed\u8a00\u9519\u8bef\u7801\u7684\u5bf9\u6bd4\u51fa\u53d1&#xff0c;\u4f9d\u6b21\u8bb2\u89e3\u5f02\u5e38\u7684\u629b\u51fa\u4e0e\u6355\u83b7\u3001\u6808\u5c55\u5f00\u3001\u5339\u914d\u89c4\u5219\u3001\u5f02\u5e38\u91cd\u65b0\u629b\u51fa\u3001\u5f02\u5e38\u5b89\u5168\u95ee\u9898\u4ee5\u53ca\u5f02\u5e38\u89c4\u8303&#xff0c;\u5e76\u7ed3\u5408\u6807\u51c6\u5e93\u5f02\u5e38\u4f53\u7cfb\u7ed9\u51fa\u5b8c\u6574\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b&#xff0c;\u5e2e\u52a9\u8bfb\u8005\u5efa\u7acb\u5bf9 C \u5f02\u5e38\u5904\u7406\u7684\u6574\u4f53\u8ba4\u77e5\u3002<br \/>\n1. \u5f02\u5e38\u7684\u6982\u5ff5<br \/>\n\u5f02\u5e38\u5904\u7406\u673a\u5236\u5141\u8bb8\u7a0b\u5e8f\u4e2d\u72ec\u7acb\u5f00\u53d1\u7684\u90e8\u5206&#xff0c;\u5728\u8fd0\u884c\u65f6\u5bf9\u51fa\u73b0\u7684\u95ee\u9898\u8fdb\u884c\u901a\u4fe1\u5e76\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002\u5f02\u5e38\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u5c06\u95ee\u9898\u7684\u68c0\u6d4b\u4e0e\u95ee\u9898<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,427],"topic":[],"class_list":["post-120650","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ \u5f02\u5e38\u673a\u5236\u8be6\u89e3\uff1a\u4ece\u629b\u51fa\u6355\u83b7\u5230\u5f02\u5e38\u5b89\u5168 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/120650.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ \u5f02\u5e38\u673a\u5236\u8be6\u89e3\uff1a\u4ece\u629b\u51fa\u6355\u83b7\u5230\u5f02\u5e38\u5b89\u5168 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u68b3\u7406 C \u5f02\u5e38\u5904\u7406\u673a\u5236&#xff0c;\u4ece\u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5\u4e0e C \u8bed\u8a00\u9519\u8bef\u7801\u7684\u5bf9\u6bd4\u51fa\u53d1&#xff0c;\u4f9d\u6b21\u8bb2\u89e3\u5f02\u5e38\u7684\u629b\u51fa\u4e0e\u6355\u83b7\u3001\u6808\u5c55\u5f00\u3001\u5339\u914d\u89c4\u5219\u3001\u5f02\u5e38\u91cd\u65b0\u629b\u51fa\u3001\u5f02\u5e38\u5b89\u5168\u95ee\u9898\u4ee5\u53ca\u5f02\u5e38\u89c4\u8303&#xff0c;\u5e76\u7ed3\u5408\u6807\u51c6\u5e93\u5f02\u5e38\u4f53\u7cfb\u7ed9\u51fa\u5b8c\u6574\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b&#xff0c;\u5e2e\u52a9\u8bfb\u8005\u5efa\u7acb\u5bf9 C \u5f02\u5e38\u5904\u7406\u7684\u6574\u4f53\u8ba4\u77e5\u3002 1. \u5f02\u5e38\u7684\u6982\u5ff5 \u5f02\u5e38\u5904\u7406\u673a\u5236\u5141\u8bb8\u7a0b\u5e8f\u4e2d\u72ec\u7acb\u5f00\u53d1\u7684\u90e8\u5206&#xff0c;\u5728\u8fd0\u884c\u65f6\u5bf9\u51fa\u73b0\u7684\u95ee\u9898\u8fdb\u884c\u901a\u4fe1\u5e76\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002\u5f02\u5e38\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u5c06\u95ee\u9898\u7684\u68c0\u6d4b\u4e0e\u95ee\u9898\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/120650.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-31T15:48:32+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/120650.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/120650.html\",\"name\":\"C++ \u5f02\u5e38\u673a\u5236\u8be6\u89e3\uff1a\u4ece\u629b\u51fa\u6355\u83b7\u5230\u5f02\u5e38\u5b89\u5168 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-31T15:48:32+00:00\",\"dateModified\":\"2026-10-31T15:48:32+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/120650.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/120650.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/120650.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ \u5f02\u5e38\u673a\u5236\u8be6\u89e3\uff1a\u4ece\u629b\u51fa\u6355\u83b7\u5230\u5f02\u5e38\u5b89\u5168\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ \u5f02\u5e38\u673a\u5236\u8be6\u89e3\uff1a\u4ece\u629b\u51fa\u6355\u83b7\u5230\u5f02\u5e38\u5b89\u5168 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/120650.html","og_locale":"zh_CN","og_type":"article","og_title":"C++ \u5f02\u5e38\u673a\u5236\u8be6\u89e3\uff1a\u4ece\u629b\u51fa\u6355\u83b7\u5230\u5f02\u5e38\u5b89\u5168 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u68b3\u7406 C \u5f02\u5e38\u5904\u7406\u673a\u5236&#xff0c;\u4ece\u5f02\u5e38\u7684\u57fa\u672c\u6982\u5ff5\u4e0e C \u8bed\u8a00\u9519\u8bef\u7801\u7684\u5bf9\u6bd4\u51fa\u53d1&#xff0c;\u4f9d\u6b21\u8bb2\u89e3\u5f02\u5e38\u7684\u629b\u51fa\u4e0e\u6355\u83b7\u3001\u6808\u5c55\u5f00\u3001\u5339\u914d\u89c4\u5219\u3001\u5f02\u5e38\u91cd\u65b0\u629b\u51fa\u3001\u5f02\u5e38\u5b89\u5168\u95ee\u9898\u4ee5\u53ca\u5f02\u5e38\u89c4\u8303&#xff0c;\u5e76\u7ed3\u5408\u6807\u51c6\u5e93\u5f02\u5e38\u4f53\u7cfb\u7ed9\u51fa\u5b8c\u6574\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b&#xff0c;\u5e2e\u52a9\u8bfb\u8005\u5efa\u7acb\u5bf9 C \u5f02\u5e38\u5904\u7406\u7684\u6574\u4f53\u8ba4\u77e5\u3002 1. \u5f02\u5e38\u7684\u6982\u5ff5 \u5f02\u5e38\u5904\u7406\u673a\u5236\u5141\u8bb8\u7a0b\u5e8f\u4e2d\u72ec\u7acb\u5f00\u53d1\u7684\u90e8\u5206&#xff0c;\u5728\u8fd0\u884c\u65f6\u5bf9\u51fa\u73b0\u7684\u95ee\u9898\u8fdb\u884c\u901a\u4fe1\u5e76\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002\u5f02\u5e38\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u5c06\u95ee\u9898\u7684\u68c0\u6d4b\u4e0e\u95ee\u9898","og_url":"https:\/\/www.wsisp.com\/helps\/120650.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-31T15:48:32+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/120650.html","url":"https:\/\/www.wsisp.com\/helps\/120650.html","name":"C++ \u5f02\u5e38\u673a\u5236\u8be6\u89e3\uff1a\u4ece\u629b\u51fa\u6355\u83b7\u5230\u5f02\u5e38\u5b89\u5168 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-31T15:48:32+00:00","dateModified":"2026-10-31T15:48:32+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/120650.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/120650.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/120650.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++ \u5f02\u5e38\u673a\u5236\u8be6\u89e3\uff1a\u4ece\u629b\u51fa\u6355\u83b7\u5230\u5f02\u5e38\u5b89\u5168"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/120650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=120650"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/120650\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=120650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=120650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=120650"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=120650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}