<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>素心如何天上月 &#187; exception</title> <atom:link href="http://yongsun.me/tag/exception/feed/" rel="self" type="application/rss+xml" /><link>http://yongsun.me</link> <description>Yong Sun&#039;s Blog</description> <lastBuildDate>Sat, 17 Dec 2011 16:17:35 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3</generator> <item><title>C++构造函数抛出的异常</title><link>http://yongsun.me/2009/12/c%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e6%8a%9b%e5%87%ba%e7%9a%84%e5%bc%82%e5%b8%b8/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=c%25e6%259e%2584%25e9%2580%25a0%25e5%2587%25bd%25e6%2595%25b0%25e6%258a%259b%25e5%2587%25ba%25e7%259a%2584%25e5%25bc%2582%25e5%25b8%25b8</link> <comments>http://yongsun.me/2009/12/c%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e6%8a%9b%e5%87%ba%e7%9a%84%e5%bc%82%e5%b8%b8/#comments</comments> <pubDate>Thu, 17 Dec 2009 07:16:34 +0000</pubDate> <dc:creator>yongsun</dc:creator> <category><![CDATA[c++]]></category> <category><![CDATA[constructor]]></category> <category><![CDATA[cpp]]></category> <category><![CDATA[exception]]></category> <category><![CDATA[lessons learn]]></category> <guid
isPermaLink="false">http://yongsun.me/?p=1090</guid> <description><![CDATA[要处理好C++构造函数抛出异常，的确是很tricky啊。 如果要分配在堆上，例如 T* p = new T()，则new operator会被异常中断，造成p没有被赋值。在离开构造函数的scope时，首先会析构自己的成员变量，并会递归调用父类的析构函数；不过，自己的析构函数是没有机会执行了，即便它被分配到栈上。唯一值得欣慰的是，为这个对象分配的内存，是会被释放掉的。 因此如果在构造函数中要分配一些资源，并且在执行过程中可能会抛出异常，最好用auto_ptr把它们保护起来。或者干脆不要在构造函数中执行复杂的初始化操作，转而定义一个单独的initialize方法 ...]]></description> <content:encoded><![CDATA[<p>要处理好C++构造函数抛出异常，的确是很tricky啊。</p><p>如果要分配在堆上，例如 <code>T* p = new T()</code>，则new operator会被异常中断，造成p没有被赋值。在离开构造函数的scope时，首先会析构自己的成员变量，并会递归调用父类的析构函数；不过，自己的析构函数是没有机会执行了，即便它被分配到栈上。唯一值得欣慰的是，为这个对象分配的内存，是会被释放掉的。</p><p>因此如果在构造函数中要分配一些资源，并且在执行过程中可能会抛出异常，最好用<code>auto_ptr</code>把它们保护起来。或者干脆不要在构造函数中执行复杂的初始化操作，转而定义一个单独的<code>initialize</code>方法 ...</p> ]]></content:encoded> <wfw:commentRss>http://yongsun.me/2009/12/c%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e6%8a%9b%e5%87%ba%e7%9a%84%e5%bc%82%e5%b8%b8/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
