<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx</link><description>An interesting debate about C++ exceptions took place a few weeks ago in the C++ MVPs discussion list. The trigger was something as innocent and specific as &amp;ldquo; would you guys just throw std::runtime_exception with some error message string or you</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx#10134363</link><pubDate>Sat, 26 Feb 2011 02:17:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10134363</guid><dc:creator>Jason</dc:creator><description>&lt;p&gt;I use scopeguard for simple code that might be handled by finally in Java. I prefer it to finally because (a) the release code only ever needs to be mentioned once and (b) the release code is written immediately after the acquisition code so it&amp;#39;s easier to maintain.&lt;/p&gt;
&lt;p&gt;E.g.:&lt;/p&gt;
&lt;p&gt;	CDC* pDC = GetDC();&lt;/p&gt;
&lt;p&gt;	ON_BLOCK_EXIT(&amp;amp;CWnd::ReleaseDC, this, pDC);&lt;/p&gt;
&lt;p&gt;The original article is at &lt;a rel="nofollow" target="_new" href="http://www.drdobbs.com/184403758"&gt;www.drdobbs.com/184403758&lt;/a&gt; but I use the one by Joshua Lehrer (&lt;a rel="nofollow" target="_new" href="http://www.zete.org/people/jlehrer/scopeguard.html"&gt;www.zete.org/.../scopeguard.html&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve come across a really simple C++0x implementation using lambdas but I&amp;#39;m not sure where I got it from now. But it allows you to handle the example given by kaalus like this:&lt;/p&gt;
&lt;p&gt;	void f()&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		executing = true;&lt;/p&gt;
&lt;p&gt;		ON_BLOCK_EXIT([&amp;amp;]{&lt;/p&gt;
&lt;p&gt;			executing = false;&lt;/p&gt;
&lt;p&gt;		});&lt;/p&gt;
&lt;p&gt;		DoStuff();&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;Much more elegant and easier to read than finally.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10134363" width="1" height="1"&gt;</description></item><item><title>re: Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx#10128666</link><pubDate>Sun, 13 Feb 2011 08:23:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10128666</guid><dc:creator>Leo Davidson</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt; Should we be forced to wrap every single thing we use&lt;/p&gt;
&lt;p&gt;&amp;gt; Yes. Yes, you should.&lt;/p&gt;
&lt;p&gt;Expecting every single development team to make their own bespoke wrappers seems backwards.&lt;/p&gt;
&lt;p&gt;How about the people who make Win32, the main non-RAII API we are all subjected to, do that C++ RAII wrapper for us, well and in one place, make it cover the entire API, and continue to maintain it as part of the main SDK, not as an afterthought that is updated slowly and all but abandoned when some other framework becomes the current fad?&lt;/p&gt;
&lt;p&gt;Aside from the consolidation of effort, it&amp;#39;d make code and programmers a lot more interchangeable between projects and teams.&lt;/p&gt;
&lt;p&gt;If MS cannot provide that wrapper is it reasonable to expect everyone else to provide their own?&lt;/p&gt;
&lt;p&gt;Smart pointers and other generics are simply not enough to deal with all the weird things Win32 throws at us.&lt;/p&gt;
&lt;p&gt;FWIW, I *do* make RAII wrappers for a lot of things (and not just resource clean-up; e.g. ensuring a threading event is set when a code block exits), and in my own code I almost never use exceptions, but I&amp;#39;ve still found myself in situations where I&amp;#39;ve sat wondering why C++ so stubbornly resists having finally blocks. It seems like a head-in-the-sand mentality to me, which is quite strange for a language that bends over backwards to support so many different programming styles (and as a result, supports Win32, which is not RAII).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10128666" width="1" height="1"&gt;</description></item><item><title>re: Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx#10126435</link><pubDate>Tue, 08 Feb 2011 21:17:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10126435</guid><dc:creator>dex</dc:creator><description>&lt;p&gt;Who needs finally?&lt;/p&gt;
&lt;p&gt;That&amp;#39;s what the lines after the catch clauses are for aren&amp;#39;t they?&lt;/p&gt;
&lt;p&gt;What do you mean there are some exceptions you just let through?&lt;/p&gt;
&lt;p&gt;Then I put it to you that your supposed &amp;quot;finally&amp;quot; code is (usually) not going to make one whit of difference, you&amp;#39;re already in a bad place.&lt;/p&gt;
&lt;p&gt;Who uses global variables directly rather than wrapping them properly?&lt;/p&gt;
&lt;p&gt;and yes, so far I&amp;#39;ve wrapped a fair percentage of the Win32 API, god help me...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10126435" width="1" height="1"&gt;</description></item><item><title>re: Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx#10126428</link><pubDate>Tue, 08 Feb 2011 21:11:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10126428</guid><dc:creator>dex</dc:creator><description>&lt;p&gt;Internationalisation in std::exception?&lt;/p&gt;
&lt;p&gt;Use UTF-8 (or if you&amp;#39;re really pedantic about signed char UTF-7).&lt;/p&gt;
&lt;p&gt;A simple wrapper class can take care of the conversion if you insist on using Unicode strings or you better still use a resource string id (or equivalent) and a look-up/load operation etc. etc.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10126428" width="1" height="1"&gt;</description></item><item><title>re: Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx#10125935</link><pubDate>Tue, 08 Feb 2011 00:26:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10125935</guid><dc:creator>kaalus</dc:creator><description>&lt;p&gt;Finally is sorely lacking from C++. RAII is great but it only really applies to releasing resources wrapped in a class. What if e.g. I want to reliably reset a flag on exit from a function?&lt;/p&gt;
&lt;p&gt;void f()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;executing = true;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;try&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;DoStuff();&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;finally&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;executing = false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Instead of the clean code above, in C++ I would have to create a silly class that resets the flag in the destructor.&lt;/p&gt;
&lt;p&gt;No idea why some people are so stubborn on that one. Just add finally, it&amp;#39;s not redundant by any means.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10125935" width="1" height="1"&gt;</description></item><item><title>re: Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx#10112736</link><pubDate>Fri, 07 Jan 2011 00:01:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10112736</guid><dc:creator>Diego Dagum - MSFT</dc:creator><description>&lt;p&gt;I want to thank you all guys for all these comments. They certainly reconfirm once again that there&amp;#39;s no one-size-fits-all clue for any potential problem that requires exception handling.&lt;/p&gt;
&lt;p&gt;But that&amp;#39;s not a fact to get frustrated: it&amp;#39;s just some wisdom to correctly tailor, whatever the approach we take, in its right size (helpful for these contexts, harmful for another ones).&lt;/p&gt;
&lt;p&gt;I learned a lot from you guys. Will keep posting these debates.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10112736" width="1" height="1"&gt;</description></item><item><title>re: Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx#10111236</link><pubDate>Mon, 03 Jan 2011 19:56:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10111236</guid><dc:creator>S. Colcord</dc:creator><description>&lt;p&gt;STL wrote:&lt;/p&gt;
&lt;p&gt;&amp;quot;...with /EHsc being preferable (it&amp;#39;s faster because it assumes that extern &amp;quot;C&amp;quot; functions won&amp;#39;t emit exceptions - while technically permitted by the Standard, sane code should never attempt to do such a thing...&amp;quot;&lt;/p&gt;
&lt;p&gt;While it would certainly be preferable to avoid this, it sometimes happens. &amp;nbsp;I&amp;#39;m maintaining a large (several million LOC), old (25+ years) application, written in C and partially converted to C++, where the call stack regularly passes back and forth between the two. &amp;nbsp;It just wouldn&amp;#39;t be practical to try to block exceptions from crossing the language boundary. &amp;nbsp;We just spent a substantial amount of time tracking down a problem that turned out to be because we were using /EHsc. &amp;nbsp;I would recommend that all mixed C and C++ applications use /EHs by default, and only switch to /EHsc if they&amp;#39;re really certain that their extern &amp;quot;C&amp;quot; functions could never let an exception escape (presumably with a catch(...) clause). &amp;nbsp;Otherwise the optimizer starts silently eliding catch clauses, resulting in Release-build only crashes that can be hard to track down.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10111236" width="1" height="1"&gt;</description></item><item><title>re: Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx#10111038</link><pubDate>Mon, 03 Jan 2011 08:06:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10111038</guid><dc:creator>non-unicode :(</dc:creator><description>&lt;p&gt;The problem I&amp;#39;ve had with the STL exceptions is that they store the message as a byte (non-Unicode) string (or at least, std::exception::what returns a byte string). So I create my own base exception class:&lt;/p&gt;
&lt;p&gt;class Exception&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;public:&lt;/p&gt;
&lt;p&gt;	explicit Exception(const std::wstring&amp;amp; message) : m_message(message)&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	virtual ~Exception()&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	const std::wstring&amp;amp; GetMessage() const&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		return m_message;&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;protected:&lt;/p&gt;
&lt;p&gt;	std::wstring m_message;&lt;/p&gt;
&lt;p&gt;};&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10111038" width="1" height="1"&gt;</description></item><item><title>re: Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx#10110837</link><pubDate>Sat, 01 Jan 2011 23:22:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10110837</guid><dc:creator>Marat Abrarov</dc:creator><description>&lt;p&gt;What about boost::asio &amp;amp;&amp;amp; boost::system::error_code? &lt;/p&gt;
&lt;p&gt;Does anybody remember its trick with exceptions vs error codes? I think it&amp;#39;s a great example of how it must be written in &amp;quot;modern C++ style&amp;quot; - use error codes (result codes) for code with (often) variable results (i.e. boost::system::error_code) and write exception-based wrappers for those users who don&amp;#39;t need error codes. It&amp;#39;s very simple (but mostly reasonable for libraries). &lt;/p&gt;
&lt;p&gt;And, of course, sometimes error codes can&amp;#39;t be used (we all know this cases - constructors, operators&amp;#39; overloading etc).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10110837" width="1" height="1"&gt;</description></item><item><title>re: Making an Exception</title><link>http://blogs.msdn.com/b/vcblog/archive/2010/12/29/making-an-exception.aspx#10110771</link><pubDate>Sat, 01 Jan 2011 13:27:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10110771</guid><dc:creator>Gary Price</dc:creator><description>&lt;p&gt;The best reason I know not to allocate memory when throwing exceptions is to avoid a crash if the heap is corrupt, leading to a nasty debugging situation where you can&amp;#39;t get information about what went wrong. &amp;nbsp;The same thing goes for freeing memory. It also applies to operations that you might undertake to document what went wrong - for example dumping a file of information. All should be done without involving the heap. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10110771" width="1" height="1"&gt;</description></item></channel></rss>