<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[请教如何自定义异常]]></title><description><![CDATA[<p dir="auto">我是一个会一点C++的菜鸟，我需要在自己的代码里面自定义一个异常类，然后我的代码的写法类似于下面这样：</p>
<pre><code class="language-c++">#include &lt;exception&gt;

class myException : public std::exception {
	private:
		const char * msg;
	public:
		myException(const char* message) noexcept : msg(message){}
		const char * what() const noexcept override{
			return msg;
		}
};
</code></pre>
<p dir="auto">感觉这样写好像不太对，自己看了一下<code>std::exception</code>、<code>std::runtime_error</code>、<code>std::logic_error</code>的代码，发现<code>std::runtime_error</code>和<code>std::logic_error</code>都是用<code>__cow_string</code>这个结构体来存储字符串信息的，代码如下：</p>
<pre><code class="language-cpp">struct __cow_string
  {
    union {
      const char* _M_p;
      char _M_bytes[sizeof(const char*)];
    };

    __cow_string();
    __cow_string(const std::string&amp;);
    __cow_string(const char*, size_t);
    __cow_string(const __cow_string&amp;) _GLIBCXX_NOTHROW;
    __cow_string&amp; operator=(const __cow_string&amp;) _GLIBCXX_NOTHROW;
    ~__cow_string();
   __cow_string(__cow_string&amp;&amp;) noexcept;
    __cow_string&amp; operator=(__cow_string&amp;&amp;) noexcept;
};
</code></pre>
<p dir="auto">貌似只有声明，看不到实现。<br />
所以到这里来请教一下。</p>
]]></description><link>http://forum.d2learn.org/topic/182/请教如何自定义异常</link><generator>RSS for Node</generator><lastBuildDate>Sat, 12 Sep 2026 10:52:25 GMT</lastBuildDate><atom:link href="http://forum.d2learn.org/topic/182.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 29 Mar 2026 05:14:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to 请教如何自定义异常 on Tue, 31 Mar 2026 07:28:12 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">class myException : public std::exception {
private:
    std::string _msg;
public:
    myException(const char* message) noexcept : _msg(message) {}
    
    const char* what() const noexcept override {
        return _msg.c_str();
    }
};
</code></pre>
]]></description><link>http://forum.d2learn.org/post/809</link><guid isPermaLink="true">http://forum.d2learn.org/post/809</guid><dc:creator><![CDATA[SPeak]]></dc:creator><pubDate>Tue, 31 Mar 2026 07:28:12 GMT</pubDate></item><item><title><![CDATA[Reply to 请教如何自定义异常 on Tue, 31 Mar 2026 07:25:33 GMT]]></title><description><![CDATA[<p dir="auto">就你上面写的就可以啊</p>
]]></description><link>http://forum.d2learn.org/post/808</link><guid isPermaLink="true">http://forum.d2learn.org/post/808</guid><dc:creator><![CDATA[SPeak]]></dc:creator><pubDate>Tue, 31 Mar 2026 07:25:33 GMT</pubDate></item><item><title><![CDATA[Reply to 请教如何自定义异常 on Tue, 31 Mar 2026 05:37:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="mention plugin-mentions-user plugin-mentions-a" href="http://forum.d2learn.org/uid/3">@SPeak</a> 那正确的写法是什么样的呢？我现在是直接继承<code>std::runtime_error</code>类，如果我不想继承这运行时错误、逻辑错误两个类，想要直接继承自<code>std::exception</code>呢？</p>
]]></description><link>http://forum.d2learn.org/post/807</link><guid isPermaLink="true">http://forum.d2learn.org/post/807</guid><dc:creator><![CDATA[wlly-lzh]]></dc:creator><pubDate>Tue, 31 Mar 2026 05:37:40 GMT</pubDate></item><item><title><![CDATA[Reply to 请教如何自定义异常 on Mon, 30 Mar 2026 08:17:22 GMT]]></title><description><![CDATA[<p dir="auto">const char * msg; 指针可能会关联到外部对象(不可预期的生命周期), 可以使用 std::string 自己持有对象, 确保生命周期安全. 对关于 __cow_string 这个是 内部实现的 字符串的写时复制 机制, 可以不用管</p>
<p dir="auto">可能的风险点</p>
<pre><code class="language-cpp">{
    std::string localVar = "Hi, I'm local var";
    throw myException(localVar.c_str());  // localVar 出作用域 会析构, 可能会导致msg指向无效内存
}
</code></pre>
<ul>
<li><a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/libstdc++/api/a00605_source.html" rel="nofollow ugc">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/libstdc++/api/a00605_source.html</a></li>
</ul>
]]></description><link>http://forum.d2learn.org/post/805</link><guid isPermaLink="true">http://forum.d2learn.org/post/805</guid><dc:creator><![CDATA[SPeak]]></dc:creator><pubDate>Mon, 30 Mar 2026 08:17:22 GMT</pubDate></item></channel></rss>