{"id":78760,"date":"2026-02-28T06:32:46","date_gmt":"2026-02-27T22:32:46","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/78760.html"},"modified":"2026-02-28T06:32:46","modified_gmt":"2026-02-27T22:32:46","slug":"c%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e5%85%a8%e6%94%bb%e7%95%a5%ef%bc%9a%e6%8d%95%e8%8e%b7%e3%80%81%e8%87%aa%e5%ae%9a%e4%b9%89%e4%b8%8e%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/78760.html","title":{"rendered":"C++\u5f02\u5e38\u5904\u7406\u5168\u653b\u7565\uff1a\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u4e0e\u5b9e\u6218"},"content":{"rendered":"<h4>C&#043;&#043;\u5f02\u5e38\u5904\u7406\u673a\u5236&#xff1a;\u5f02\u5e38\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u5b9e\u6218\u5e94\u7528<\/h4>\n<h5>\u4e00\u3001\u5f02\u5e38\u6355\u83b7\u673a\u5236<\/h5>\n<p>C&#043;&#043;\u901a\u8fc7try-catch\u5757\u5b9e\u73b0\u5f02\u5e38\u6355\u83b7&#xff1a;<\/p>\n<p>try {<br \/>\n    \/\/ \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801<br \/>\n    throw std::runtime_error(&#034;\u793a\u4f8b\u5f02\u5e38&#034;);<br \/>\n}<br \/>\ncatch (const std::exception&amp; e) {<br \/>\n    std::cerr &lt;&lt; &#034;\u6355\u83b7\u5f02\u5e38: &#034; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n}<br \/>\ncatch (&#8230;) { \/\/ \u6355\u83b7\u6240\u6709\u672a\u5904\u7406\u7684\u5f02\u5e38<br \/>\n    std::cerr &lt;&lt; &#034;\u672a\u77e5\u5f02\u5e38&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<ul>\n<li>try\u5757&#xff1a;\u5305\u88f9\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801<\/li>\n<li>catch\u5757&#xff1a;\u6309\u7c7b\u578b\u5339\u914d\u5f02\u5e38&#xff0c;\u652f\u6301\u591a\u7ea7\u6355\u83b7<\/li>\n<li>throw&#xff1a;\u663e\u5f0f\u629b\u51fa\u5f02\u5e38\u5bf9\u8c61&#xff08;\u5982\u5185\u7f6e\u7c7b\u578b\u6216\u81ea\u5b9a\u4e49\u7c7b&#xff09;<\/li>\n<\/ul>\n<h5>\u4e8c\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h5>\n<p>\u901a\u8fc7\u7ee7\u627f\u6807\u51c6\u5f02\u5e38\u7c7b\u5b9e\u73b0&#xff1a;<\/p>\n<p>class CustomException : public std::exception {<br \/>\npublic:<br \/>\n    explicit CustomException(const char* msg) : msg_(msg) {}<br \/>\n    const char* what() const noexcept override {<br \/>\n        return msg_;<br \/>\n    }<br \/>\nprivate:<br \/>\n    const char* msg_;<br \/>\n};<\/p>\n<p>\/\/ \u4f7f\u7528\u793a\u4f8b<br \/>\nthrow CustomException(&#034;\u81ea\u5b9a\u4e49\u5f02\u5e38\u4fe1\u606f&#034;);<\/p>\n<ul>\n<li>\u5fc5\u987b\u91cd\u5199what()\u65b9\u6cd5\u8fd4\u56de\u5f02\u5e38\u63cf\u8ff0<\/li>\n<li>\u5efa\u8bae\u7ee7\u627fstd::runtime_error\u6216std::logic_error<\/li>\n<\/ul>\n<h5>\u4e09\u3001\u5b9e\u6218\u5e94\u7528\u573a\u666f<\/h5>\n<li>\u8d44\u6e90\u7ba1\u7406\u5f02\u5e38<\/li>\n<p>void readFile(const std::string&amp; path) {<br \/>\n    std::ifstream file(path);<br \/>\n    if (!file.is_open())<br \/>\n        throw std::ios_base::failure(&#034;\u6587\u4ef6\u6253\u5f00\u5931\u8d25&#034;);<\/p>\n<p>    \/\/ RAII\u673a\u5236\u786e\u4fdd\u8d44\u6e90\u91ca\u653e<br \/>\n    \/\/ &#8230;<br \/>\n}<\/p>\n<li>\u53c2\u6570\u6821\u9a8c\u5f02\u5e38<\/li>\n<p>double divide(int a, int b) {<br \/>\n    if (b &#061;&#061; 0)<br \/>\n        throw std::invalid_argument(&#034;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&#034;);<br \/>\n    return static_cast&lt;double&gt;(a) \/ b;<br \/>\n}<\/p>\n<li>\u7cfb\u7edf\u8c03\u7528\u5f02\u5e38<\/li>\n<p>void connectDatabase() {<br \/>\n    if (!db_connection.establish())<br \/>\n        throw DatabaseException(&#034;\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25&#034;, error_code);<br \/>\n}<\/p>\n<h5>\u56db\u3001\u5f02\u5e38\u5b89\u5168\u7b49\u7ea7<\/h5>\n<li>\u57fa\u672c\u4fdd\u8bc1&#xff1a;\u5f02\u5e38\u53d1\u751f\u540e\u5bf9\u8c61\u4ecd\u5904\u4e8e\u6709\u6548\u72b6\u6001<\/li>\n<li>\u5f3a\u4fdd\u8bc1&#xff1a;\u64cd\u4f5c\u8981\u4e48\u5b8c\u5168\u6210\u529f&#xff0c;\u8981\u4e48\u72b6\u6001\u56de\u6eda<\/li>\n<li>\u4e0d\u629b\u4fdd\u8bc1&#xff1a;\u4f7f\u7528noexcept\u58f0\u660e\u4e0d\u629b\u51fa\u5f02\u5e38<\/li>\n<p>void criticalOperation() noexcept; \/\/ C&#043;&#043;11\u8d77\u652f\u6301<\/p>\n<h5>\u4e94\u3001\u6700\u4f73\u5b9e\u8df5<\/h5>\n<li>\u907f\u514d\u5728\u6790\u6784\u51fd\u6570\u4e2d\u629b\u51fa\u5f02\u5e38<\/li>\n<li>\u4f7f\u7528\u667a\u80fd\u6307\u9488\u7ba1\u7406\u8d44\u6e90&#xff08;\u5982std::unique_ptr&#xff09;<\/li>\n<li>\u4f18\u5148\u9009\u62e9\u6807\u51c6\u5f02\u5e38\u7c7b\u578b&#xff1a;\n<ul>\n<li>std::runtime_error&#xff1a;\u8fd0\u884c\u65f6\u9519\u8bef<\/li>\n<li>std::logic_error&#xff1a;\u903b\u8f91\u9519\u8bef<\/li>\n<li>std::out_of_range&#xff1a;\u8d8a\u754c\u8bbf\u95ee<\/li>\n<\/ul>\n<\/li>\n<li>\u5f02\u5e38\u7c7b\u578b\u5c42\u7ea7&#xff1a; $$ \\\\begin{array}{c} \\\\text{std::exception} \\\\ \\\\downarrow \\\\ \\\\text{std::runtime_error} \\\\ \\\\downarrow \\\\ \\\\text{CustomException} \\\\end{array} $$<\/li>\n<p>\u6ce8&#xff1a;\u5f02\u5e38\u5904\u7406\u4f1a\u589e\u52a0\u7ea65-10%\u7684\u6027\u80fd\u5f00\u9500&#xff0c;\u5173\u952e\u6027\u80fd\u6a21\u5757\u53ef\u4f7f\u7528\u9519\u8bef\u7801\u66ff\u4ee3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C\u5f02\u5e38\u5904\u7406\u673a\u5236&#xff1a;\u5f02\u5e38\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u5b9e\u6218\u5e94\u7528\u4e00\u3001\u5f02\u5e38\u6355\u83b7\u673a\u5236C\u901a\u8fc7try-catch\u5757\u5b9e\u73b0\u5f02\u5e38\u6355\u83b7&#xff1a;try {\/\/ \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801throw std::runtime_error(\\&#8221;\u793a\u4f8b\u5f02\u5e38\\&#8221;);<br \/>\n}<br \/>\ncatch (const std::exception&amp; e) {std::cerr &lt;&lt; \\&quot;\u6355\u83b7\u5f02\u5e38: \\&quot; &lt;&lt; e.what() &lt;&lt; std::endl;<br \/>\n}<br \/>\ncatch (&#8230;) { \/\/ \u6355\u83b7\u6240\u6709\u672a\u5904\u7406\u7684\u5f02\u5e38std::cerr &lt;<\/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,190],"topic":[],"class_list":["post-78760","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","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\u5168\u653b\u7565\uff1a\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u4e0e\u5b9e\u6218 - \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\/78760.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\u5168\u653b\u7565\uff1a\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"C\u5f02\u5e38\u5904\u7406\u673a\u5236&#xff1a;\u5f02\u5e38\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u5b9e\u6218\u5e94\u7528\u4e00\u3001\u5f02\u5e38\u6355\u83b7\u673a\u5236C\u901a\u8fc7try-catch\u5757\u5b9e\u73b0\u5f02\u5e38\u6355\u83b7&#xff1a;try {\/\/ \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801throw std::runtime_error(&quot;\u793a\u4f8b\u5f02\u5e38&quot;); } catch (const std::exception&amp; e) {std::cerr &lt;&lt; &quot;\u6355\u83b7\u5f02\u5e38: &quot; &lt;&lt; e.what() &lt;&lt; std::endl; } catch (...) { \/\/ \u6355\u83b7\u6240\u6709\u672a\u5904\u7406\u7684\u5f02\u5e38std::cerr &lt;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/78760.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-27T22:32:46+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\/78760.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/78760.html\",\"name\":\"C++\u5f02\u5e38\u5904\u7406\u5168\u653b\u7565\uff1a\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-27T22:32:46+00:00\",\"dateModified\":\"2026-02-27T22:32:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/78760.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/78760.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78760.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\u5168\u653b\u7565\uff1a\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u4e0e\u5b9e\u6218\"}]},{\"@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\u5168\u653b\u7565\uff1a\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u4e0e\u5b9e\u6218 - \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\/78760.html","og_locale":"zh_CN","og_type":"article","og_title":"C++\u5f02\u5e38\u5904\u7406\u5168\u653b\u7565\uff1a\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"C\u5f02\u5e38\u5904\u7406\u673a\u5236&#xff1a;\u5f02\u5e38\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u5b9e\u6218\u5e94\u7528\u4e00\u3001\u5f02\u5e38\u6355\u83b7\u673a\u5236C\u901a\u8fc7try-catch\u5757\u5b9e\u73b0\u5f02\u5e38\u6355\u83b7&#xff1a;try {\/\/ \u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801throw std::runtime_error(\"\u793a\u4f8b\u5f02\u5e38\"); } catch (const std::exception&amp; e) {std::cerr &lt;&lt; &quot;\u6355\u83b7\u5f02\u5e38: &quot; &lt;&lt; e.what() &lt;&lt; std::endl; } catch (...) { \/\/ \u6355\u83b7\u6240\u6709\u672a\u5904\u7406\u7684\u5f02\u5e38std::cerr &lt;","og_url":"https:\/\/www.wsisp.com\/helps\/78760.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-27T22:32:46+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\/78760.html","url":"https:\/\/www.wsisp.com\/helps\/78760.html","name":"C++\u5f02\u5e38\u5904\u7406\u5168\u653b\u7565\uff1a\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-27T22:32:46+00:00","dateModified":"2026-02-27T22:32:46+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/78760.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/78760.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/78760.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\u5168\u653b\u7565\uff1a\u6355\u83b7\u3001\u81ea\u5b9a\u4e49\u4e0e\u5b9e\u6218"}]},{"@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\/78760","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=78760"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78760\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=78760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=78760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=78760"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=78760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}