{"id":3319,"date":"2025-03-29T17:12:07","date_gmt":"2025-03-29T09:12:07","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3319"},"modified":"2025-03-29T17:12:07","modified_gmt":"2025-03-29T09:12:07","slug":"c-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/29\/c-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86\/","title":{"rendered":"C++ \u5f02\u5e38\u5904\u7406"},"content":{"rendered":"\n<p>C++ \u5f02\u5e38\u5904\u7406\u673a\u5236\u7528\u4e8e\u6355\u83b7\u5e76\u5904\u7406\u7a0b\u5e8f\u6267\u884c\u4e2d\u7684\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\u3002\u5f02\u5e38\u5904\u7406\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u5904\u7406\u9519\u8bef\uff0c\u800c\u4e0d\u4f1a\u76f4\u63a5\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002C++ \u7684\u5f02\u5e38\u5904\u7406\u4f7f\u7528 <code>try<\/code>\u3001<code>throw<\/code> \u548c <code>catch<\/code> \u8bed\u53e5\u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u5f02\u5e38\u5904\u7406\u57fa\u672c\u6982\u5ff5<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>throw<\/strong>\uff1a\u7528\u4e8e\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n\n\n\n<li><strong>try<\/strong>\uff1a\u7528\u4e8e\u5305\u542b\u53ef\u80fd\u53d1\u751f\u5f02\u5e38\u7684\u4ee3\u7801\u5757\u3002<\/li>\n\n\n\n<li><strong>catch<\/strong>\uff1a\u7528\u4e8e\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5f02\u5e38\u5904\u7406\u7684\u76ee\u7684\u662f\u5728\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u8865\u6551\u63aa\u65bd\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5f02\u5e38\u5904\u7406\u7684\u57fa\u672c\u8bed\u6cd5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 try-catch \u8bed\u53e5<\/strong><\/h3>\n\n\n\n<p>\u5f02\u5e38\u5904\u7406\u7684\u57fa\u672c\u7ed3\u6784\u662f <code>try<\/code> \u5757\u548c <code>catch<\/code> \u5757\u3002\u4ee3\u7801\u5757\u4e2d\u7684\u67d0\u4e9b\u64cd\u4f5c\u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0c\u5c06\u8df3\u8f6c\u5230 <code>catch<\/code> \u5757\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n    try {\n        int x = 10;\n        int y = 0;\n        if (y == 0) {\n            throw \"Division by zero error\"; \/\/ \u629b\u51fa\u5f02\u5e38\n        }\n        cout &lt;&lt; x \/ y &lt;&lt; endl;\n    }\n    catch (const char* e) {\n        \/\/ \u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\n        cout &lt;&lt; \"Error: \" &lt;&lt; e &lt;&lt; endl;\n    }\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>throw<\/code>\uff1a\u7528\u4e8e\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u53ef\u4ee5\u629b\u51fa\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5982\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5bf9\u8c61\u7b49\u3002<\/li>\n\n\n\n<li><code>catch<\/code>\uff1a\u7528\u4e8e\u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406\u3002<code>catch<\/code> \u540e\u9762\u6307\u5b9a\u5f02\u5e38\u7684\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u591a\u79cd\u7c7b\u578b\u7684\u5f02\u5e38<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728 <code>catch<\/code> \u5757\u4e2d\u6355\u83b7\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\uff0cC++ \u652f\u6301\u591a\u91cd <code>catch<\/code> \u5757\u6765\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n    try {\n        int choice = 2;\n\n        if (choice == 1) {\n            throw 10; \/\/ \u629b\u51fa\u6574\u578b\u5f02\u5e38\n        } else if (choice == 2) {\n            throw \"String exception\"; \/\/ \u629b\u51fa\u5b57\u7b26\u4e32\u5f02\u5e38\n        }\n    }\n    catch (int e) {\n        cout &lt;&lt; \"Caught an integer exception: \" &lt;&lt; e &lt;&lt; endl;\n    }\n    catch (const char* e) {\n        cout &lt;&lt; \"Caught a string exception: \" &lt;&lt; e &lt;&lt; endl;\n    }\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Caught a string exception: String exception<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f02\u5e38\u662f\u6309\u987a\u5e8f\u5339\u914d\u7684\uff0c\u5982\u679c\u6355\u83b7\u5230\u7c7b\u578b\u5339\u914d\u7684\u5f02\u5e38\uff0c\u5c31\u6267\u884c\u76f8\u5e94\u7684 <code>catch<\/code> \u5757\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u629b\u51fa\u5f02\u5e38\uff08throw\uff09<\/strong><\/h2>\n\n\n\n<p>\u5728 C++ \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>throw<\/code> \u5173\u952e\u5b57\u629b\u51fa\u5f02\u5e38\u3002\u4f60\u53ef\u4ee5\u629b\u51fa\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u7c7b\u5bf9\u8c61\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u629b\u51fa\u5185\u5efa\u7c7b\u578b\u7684\u5f02\u5e38<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n    try {\n        throw 404;  \/\/ \u629b\u51fa\u6574\u578b\u5f02\u5e38\n    }\n    catch (int e) {\n        cout &lt;&lt; \"Caught exception: \" &lt;&lt; e &lt;&lt; endl;\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u629b\u51fa\u7c7b\u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u7c7b\uff0c\u5e76\u629b\u51fa\u7c7b\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass MyException {\npublic:\n    void message() {\n        cout &lt;&lt; \"This is a custom exception!\" &lt;&lt; endl;\n    }\n};\n\nint main() {\n    try {\n        throw MyException(); \/\/ \u629b\u51fa\u81ea\u5b9a\u4e49\u7c7b\u5bf9\u8c61\n    }\n    catch (MyException e) {\n        e.message(); \/\/ \u6355\u83b7\u5e76\u8c03\u7528\u7c7b\u7684\u6210\u5458\u51fd\u6570\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u5f02\u5e38\u7684\u4f20\u64ad\uff08\u5f02\u5e38\u7684\u4f20\u64ad\u673a\u5236\uff09<\/strong><\/h2>\n\n\n\n<p>\u5728 C++ \u4e2d\uff0c\u5f02\u5e38\u662f\u4ece <code>throw<\/code> \u8bed\u53e5\u629b\u51fa\u7684\uff0c\u5e76\u6309\u7167\u51fd\u6570\u8c03\u7528\u7684\u5806\u6808\u5c42\u7ea7\u9010\u5c42\u4f20\u64ad\uff0c\u76f4\u5230\u88ab\u67d0\u4e2a <code>catch<\/code> \u5757\u6355\u83b7\u3002\u5982\u679c\u5728\u5f53\u524d\u51fd\u6570\u4e2d\u6ca1\u6709\u6355\u83b7\u5f02\u5e38\uff0c\u5f02\u5e38\u4f1a\u7ee7\u7eed\u5411\u5916\u4f20\u64ad\uff0c\u76f4\u5230\u627e\u5230\u5408\u9002\u7684 <code>catch<\/code> \u5757\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u51fd\u6570\u4e2d\u7684\u5f02\u5e38\u4f20\u64ad<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nvoid functionA() {\n    throw \"Error in functionA\";  \/\/ \u629b\u51fa\u5f02\u5e38\n}\n\nvoid functionB() {\n    functionA();  \/\/ functionA \u629b\u51fa\u7684\u5f02\u5e38\u4f1a\u88ab\u6355\u83b7\n}\n\nint main() {\n    try {\n        functionB();  \/\/ \u5f02\u5e38\u6700\u7ec8\u4f1a\u5728 main \u51fd\u6570\u4e2d\u88ab\u6355\u83b7\n    }\n    catch (const char* e) {\n        cout &lt;&lt; \"Caught exception: \" &lt;&lt; e &lt;&lt; endl;\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Caught exception: Error in functionA<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f02\u5e38\u4ece <code>functionA<\/code> \u4e2d\u629b\u51fa\uff0c\u7ecf\u8fc7 <code>functionB<\/code> \u540e\u88ab <code>main<\/code> \u51fd\u6570\u4e2d\u7684 <code>catch<\/code> \u5757\u6355\u83b7\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u5f02\u5e38\u7684\u518d\u629b\u51fa<\/strong><\/h2>\n\n\n\n<p>\u5728 <code>catch<\/code> \u5757\u4e2d\u5904\u7406\u5b8c\u5f02\u5e38\u540e\uff0c\u53ef\u4ee5\u9009\u62e9\u518d\u6b21\u629b\u51fa\u5f02\u5e38\uff0c\u5c06\u5f02\u5e38\u4f20\u9012\u5230\u5916\u90e8\uff0c\u5141\u8bb8\u66f4\u9ad8\u5c42\u7684\u4ee3\u7801\u5904\u7406\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nvoid functionA() {\n    try {\n        throw \"Error in functionA\";  \/\/ \u629b\u51fa\u5f02\u5e38\n    }\n    catch (const char* e) {\n        cout &lt;&lt; \"Caught in functionA: \" &lt;&lt; e &lt;&lt; endl;\n        throw;  \/\/ \u518d\u6b21\u629b\u51fa\u5f02\u5e38\n    }\n}\n\nint main() {\n    try {\n        functionA();  \/\/ \u8fd9\u91cc\u4f1a\u6355\u83b7 functionA \u518d\u6b21\u629b\u51fa\u7684\u5f02\u5e38\n    }\n    catch (const char* e) {\n        cout &lt;&lt; \"Caught in main: \" &lt;&lt; e &lt;&lt; endl;\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Caught in functionA: Error in functionA\nCaught in main: Error in functionA<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u5f02\u5e38\u89c4\u8303\uff08C++11 \u5f02\u5e38\u58f0\u660e\uff09<\/strong><\/h2>\n\n\n\n<p>C++11 \u5f15\u5165\u4e86\u5f02\u5e38\u89c4\u8303\uff0c\u7528\u4e8e\u6307\u5b9a\u54ea\u4e9b\u51fd\u6570\u53ef\u4ee5\u629b\u51fa\u54ea\u4e9b\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>noexcept<\/code> \u58f0\u660e\u67d0\u4e2a\u51fd\u6570\u4e0d\u629b\u51fa\u4efb\u4f55\u5f02\u5e38\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.1 noexcept<\/strong><\/h3>\n\n\n\n<p><code>noexcept<\/code> \u7528\u4e8e\u8868\u793a\u51fd\u6570\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u5982\u679c\u51fd\u6570\u58f0\u660e\u4e3a <code>noexcept<\/code>\uff0c\u7f16\u8bd1\u5668\u4f1a\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nvoid myFunction() noexcept {  \/\/ \u58f0\u660e\u8be5\u51fd\u6570\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\n    cout &lt;&lt; \"This function does not throw exceptions.\" &lt;&lt; endl;\n}\n\nint main() {\n    myFunction();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.2 throw()<\/strong><\/h3>\n\n\n\n<p>\u5728 C++11 \u4e4b\u524d\uff0c<code>throw()<\/code> \u7528\u4e8e\u6307\u5b9a\u51fd\u6570\u53ef\u80fd\u629b\u51fa\u7684\u5f02\u5e38\u7c7b\u578b\u3002\u4f46\u8fd9\u79cd\u65b9\u5f0f\u5df2\u88ab <code>noexcept<\/code> \u53d6\u4ee3\uff0c<code>throw()<\/code> \u5df2\u7ecf\u8fc7\u65f6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void myFunction() throw(int) {  \/\/ \u4ec5\u629b\u51fa int \u7c7b\u578b\u5f02\u5e38\n    throw 10;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>throw<\/strong>\uff1a\u7528\u4e8e\u629b\u51fa\u5f02\u5e38\uff0c\u53ef\u4ee5\u629b\u51fa\u5185\u5efa\u7c7b\u578b\u6216\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>try-catch<\/strong>\uff1a\u7528\u4e8e\u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>\u591a\u91cd catch<\/strong>\uff1a\u53ef\u4ee5\u6355\u83b7\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u5e38\u4f20\u64ad<\/strong>\uff1a\u5f02\u5e38\u4f1a\u6cbf\u7740\u8c03\u7528\u6808\u5411\u5916\u4f20\u64ad\uff0c\u76f4\u5230\u88ab\u6355\u83b7\u3002<\/li>\n\n\n\n<li><strong>noexcept<\/strong>\uff1a\u7528\u4e8e\u6307\u5b9a\u4e00\u4e2a\u51fd\u6570\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5f02\u5e38\u5904\u7406\u673a\u5236\u4f7f\u5f97 C++ \u7a0b\u5e8f\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\uff0c\u800c\u4e0d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ \u5f02\u5e38\u5904\u7406\u673a\u5236\u7528\u4e8e\u6355\u83b7\u5e76\u5904\u7406\u7a0b\u5e8f\u6267\u884c\u4e2d\u7684\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\u3002\u5f02\u5e38\u5904\u7406\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u5904\u7406\u9519\u8bef\uff0c\u800c\u4e0d\u4f1a\u76f4\u63a5\u5bfc\u81f4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3320,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"class_list":["post-3319","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3319"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3319\/revisions"}],"predecessor-version":[{"id":3321,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3319\/revisions\/3321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3320"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}