{"id":79136,"date":"2026-03-01T08:17:40","date_gmt":"2026-03-01T00:17:40","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/79136.html"},"modified":"2026-03-01T08:17:40","modified_gmt":"2026-03-01T00:17:40","slug":"c%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e5%ae%9e%e6%88%98%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/79136.html","title":{"rendered":"C++\u5f02\u5e38\u5904\u7406\u5b9e\u6218\u6307\u5357"},"content":{"rendered":"<p>\u597d\u7684&#xff0c;\u6211\u5c06\u4e3a\u60a8\u8be6\u7ec6\u89e3\u6790 C&#043;&#043; \u7684\u5f02\u5e38\u5904\u7406\u673a\u5236&#xff0c;\u5305\u62ec\u5f02\u5e38\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u5b9e\u6218\u5e94\u7528\u3002<\/p>\n<hr \/>\n<h4>\u4e00\u3001C&#043;&#043; \u5f02\u5e38\u5904\u7406\u673a\u5236\u6982\u8ff0<\/h4>\n<p>C&#043;&#043; \u5f02\u5e38\u5904\u7406\u901a\u8fc7 try\u3001catch\u3001throw \u5173\u952e\u5b57\u5b9e\u73b0&#xff1a;<\/p>\n<ul>\n<li>throw&#xff1a;\u629b\u51fa\u5f02\u5e38\u5bf9\u8c61&#xff08;\u5982 throw MyException(&#034;error&#034;)&#xff09;\u3002<\/li>\n<li>try&#xff1a;\u5b9a\u4e49\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\u5757\u3002<\/li>\n<li>catch&#xff1a;\u6355\u83b7\u5e76\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p>try {<br \/>\n    \/\/ \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801<br \/>\n    if (error) throw std::runtime_error(&#034;Error occurred!&#034;);<br \/>\n} catch (const std::exception&amp; e) {<br \/>\n    std::cerr &lt;&lt; &#034;Caught exception: &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n}<\/p>\n<hr \/>\n<h4>\u4e8c\u3001\u5f02\u5e38\u6355\u83b7\u7684\u5c42\u6b21\u5316\u5904\u7406<\/h4>\n<p>\u901a\u8fc7\u591a\u7ea7 catch \u5757\u5b9e\u73b0\u5f02\u5e38\u5206\u7c7b\u5904\u7406&#xff1a;<\/p>\n<p>try {<br \/>\n    \/\/ &#8230;<br \/>\n} catch (const FileIOException&amp; e) {<br \/>\n    \/\/ \u5904\u7406\u6587\u4ef6 I\/O \u5f02\u5e38<br \/>\n} catch (const NetworkException&amp; e) {<br \/>\n    \/\/ \u5904\u7406\u7f51\u7edc\u5f02\u5e38<br \/>\n} catch (&#8230;) {<br \/>\n    \/\/ \u6355\u83b7\u6240\u6709\u5176\u4ed6\u5f02\u5e38<br \/>\n}<\/p>\n<hr \/>\n<h4>\u4e09\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b<\/h4>\n<p>\u901a\u8fc7\u7ee7\u627f std::exception \u5b9e\u73b0&#xff1a;<\/p>\n<p>#include &lt;stdexcept&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>class MyException : public std::exception {<br \/>\npublic:<br \/>\n    explicit MyException(const std::string&amp; msg) : message(msg) {}<br \/>\n    const char* what() const noexcept override {<br \/>\n        return message.c_str();<br \/>\n    }<br \/>\nprivate:<br \/>\n    std::string message;<br \/>\n};<\/p>\n<p>\/\/ \u4f7f\u7528\u793a\u4f8b<br \/>\nthrow MyException(&#034;Custom error message&#034;);<\/p>\n<hr \/>\n<h4>\u56db\u3001\u5b9e\u6218\u5e94\u7528&#xff1a;\u6587\u4ef6\u8bfb\u53d6\u5f02\u5e38\u5904\u7406<\/h4>\n<p>#include &lt;fstream&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;stdexcept&gt;<\/p>\n<p>void readFile(const std::string&amp; filename) {<br \/>\n    std::ifstream file(filename);<br \/>\n    if (!file) {<br \/>\n        throw std::runtime_error(&#034;Failed to open file: &#034; &#043; filename);<br \/>\n    }<br \/>\n    \/\/ \u6587\u4ef6\u5904\u7406\u903b\u8f91&#8230;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    try {<br \/>\n        readFile(&#034;data.txt&#034;);<br \/>\n    } catch (const std::exception&amp; e) {<br \/>\n        std::cerr &lt;&lt; &#034;Error: &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n        return 1;<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<hr \/>\n<h4>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h4>\n<li>\n<p>\u8d44\u6e90\u6cc4\u6f0f\u9884\u9632<br \/>\n\u4f7f\u7528 RAII&#xff08;\u5982\u667a\u80fd\u6307\u9488&#xff09;\u786e\u4fdd\u5f02\u5e38\u53d1\u751f\u65f6\u8d44\u6e90\u81ea\u52a8\u91ca\u653e&#xff1a;<\/p>\n<p>std::unique_ptr&lt;Resource&gt; res(new Resource());<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u5b89\u5168\u7b49\u7ea7<\/p>\n<ul>\n<li>\u57fa\u672c\u4fdd\u8bc1&#xff1a;\u5bf9\u8c61\u5904\u4e8e\u6709\u6548\u72b6\u6001\u3002<\/li>\n<li>\u5f3a\u5f02\u5e38\u5b89\u5168&#xff1a;\u64cd\u4f5c\u8981\u4e48\u5b8c\u5168\u6210\u529f&#xff0c;\u8981\u4e48\u72b6\u6001\u56de\u6eda\u3002<\/li>\n<li>\u4e0d\u629b\u4fdd\u8bc1&#xff1a;\u51fd\u6570\u58f0\u660e noexcept\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u907f\u514d\u6ee5\u7528<br \/>\n\u5f02\u5e38\u9002\u7528\u4e8e\u975e\u5e38\u89c4\u8def\u5f84&#xff08;\u5982\u6587\u4ef6\u4e0d\u5b58\u5728&#xff09;&#xff0c;\u800c\u975e\u5e38\u89c4\u63a7\u5236\u6d41\u3002<\/p>\n<\/li>\n<hr \/>\n<h4>\u516d\u3001\u603b\u7ed3<\/h4>\n<table>\n<tr>\u673a\u5236\u5e94\u7528\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>\u6807\u51c6\u5f02\u5e38<\/td>\n<td>\u5185\u7f6e\u9519\u8bef\u7c7b\u578b&#xff08;\u5982 std::runtime_error&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/td>\n<td>\u4e1a\u52a1\u903b\u8f91\u9519\u8bef\u5206\u7c7b<\/td>\n<\/tr>\n<tr>\n<td>noexcept<\/td>\n<td>\u4f18\u5316\u6027\u80fd&#xff0c;\u58f0\u660e\u65e0\u5f02\u5e38\u51fd\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5f02\u5e38\u5904\u7406&#xff0c;\u53ef\u63d0\u5347\u4ee3\u7801\u5065\u58ee\u6027\u4e0e\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u7684&#xff0c;\u6211\u5c06\u4e3a\u60a8\u8be6\u7ec6\u89e3\u6790 C \u7684\u5f02\u5e38\u5904\u7406\u673a\u5236&#xff0c;\u5305\u62ec\u5f02\u5e38\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u5b9e\u6218\u5e94\u7528\u3002\u4e00\u3001C \u5f02\u5e38\u5904\u7406\u673a\u5236\u6982\u8ff0C \u5f02\u5e38\u5904\u7406\u901a\u8fc7 try\u3001catch\u3001throw \u5173\u952e\u5b57\u5b9e\u73b0&#xff1a;throw&#xff1a;\u629b\u51fa\u5f02\u5e38\u5bf9\u8c61&#xff08;\u5982 throw MyException(\\&#8221;error\\&#8221;)&#xff09;\u3002try&#xff1a;\u5b9a\u4e49\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\u5757\u3002catch&#xff1a;\u6355\u83b7\u5e76\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002try {\/\/ \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801if (e<\/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":[99,190],"topic":[],"class_list":["post-79136","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++\u5f02\u5e38\u5904\u7406\u5b9e\u6218\u6307\u5357 - \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\/79136.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\u5904\u7406\u5b9e\u6218\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u597d\u7684&#xff0c;\u6211\u5c06\u4e3a\u60a8\u8be6\u7ec6\u89e3\u6790 C \u7684\u5f02\u5e38\u5904\u7406\u673a\u5236&#xff0c;\u5305\u62ec\u5f02\u5e38\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u5b9e\u6218\u5e94\u7528\u3002\u4e00\u3001C \u5f02\u5e38\u5904\u7406\u673a\u5236\u6982\u8ff0C \u5f02\u5e38\u5904\u7406\u901a\u8fc7 try\u3001catch\u3001throw \u5173\u952e\u5b57\u5b9e\u73b0&#xff1a;throw&#xff1a;\u629b\u51fa\u5f02\u5e38\u5bf9\u8c61&#xff08;\u5982 throw MyException(&quot;error&quot;)&#xff09;\u3002try&#xff1a;\u5b9a\u4e49\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\u5757\u3002catch&#xff1a;\u6355\u83b7\u5e76\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002try {\/\/ \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801if (e\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/79136.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-01T00:17:40+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=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79136.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/79136.html\",\"name\":\"C++\u5f02\u5e38\u5904\u7406\u5b9e\u6218\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-03-01T00:17:40+00:00\",\"dateModified\":\"2026-03-01T00:17:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/79136.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/79136.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79136.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++\u5f02\u5e38\u5904\u7406\u5b9e\u6218\u6307\u5357\"}]},{\"@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\u5904\u7406\u5b9e\u6218\u6307\u5357 - \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\/79136.html","og_locale":"zh_CN","og_type":"article","og_title":"C++\u5f02\u5e38\u5904\u7406\u5b9e\u6218\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u597d\u7684&#xff0c;\u6211\u5c06\u4e3a\u60a8\u8be6\u7ec6\u89e3\u6790 C \u7684\u5f02\u5e38\u5904\u7406\u673a\u5236&#xff0c;\u5305\u62ec\u5f02\u5e38\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u5b9e\u6218\u5e94\u7528\u3002\u4e00\u3001C \u5f02\u5e38\u5904\u7406\u673a\u5236\u6982\u8ff0C \u5f02\u5e38\u5904\u7406\u901a\u8fc7 try\u3001catch\u3001throw \u5173\u952e\u5b57\u5b9e\u73b0&#xff1a;throw&#xff1a;\u629b\u51fa\u5f02\u5e38\u5bf9\u8c61&#xff08;\u5982 throw MyException(\"error\")&#xff09;\u3002try&#xff1a;\u5b9a\u4e49\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\u5757\u3002catch&#xff1a;\u6355\u83b7\u5e76\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002try {\/\/ \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801if (e","og_url":"https:\/\/www.wsisp.com\/helps\/79136.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-03-01T00:17:40+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/79136.html","url":"https:\/\/www.wsisp.com\/helps\/79136.html","name":"C++\u5f02\u5e38\u5904\u7406\u5b9e\u6218\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-03-01T00:17:40+00:00","dateModified":"2026-03-01T00:17:40+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/79136.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/79136.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/79136.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++\u5f02\u5e38\u5904\u7406\u5b9e\u6218\u6307\u5357"}]},{"@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\/79136","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=79136"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/79136\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=79136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=79136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=79136"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=79136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}