<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Visual C++ exception handling</title>
	<atom:link href="http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/</link>
	<description>Software, the Internet and you.</description>
	<lastBuildDate>Fri, 10 Feb 2012 13:10:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
	<item>
		<title>By: Al.Toastman</title>
		<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/comment-page-2/#comment-115785</link>
		<dc:creator>Al.Toastman</dc:creator>
		<pubDate>Fri, 12 Nov 2010 08:03:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.thunderguy.com/plusplus/20050421/visual-c-exception-handling/#comment-115785</guid>
		<description>Thank you, from Spain!
Sorry for my english.
Very good article. It save me a migration VS2003 ==&gt; VS2008.</description>
		<content:encoded><![CDATA[<p>Thank you, from Spain!<br />
Sorry for my english.<br />
Very good article. It save me a migration VS2003 ==&gt; VS2008.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MASM Access violation handling? - CodeCall Programming Forum</title>
		<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/comment-page-2/#comment-95327</link>
		<dc:creator>MASM Access violation handling? - CodeCall Programming Forum</dc:creator>
		<pubDate>Wed, 09 Sep 2009 00:40:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.thunderguy.com/plusplus/20050421/visual-c-exception-handling/#comment-95327</guid>
		<description>[...] handling?      To answer RobotGymnast&#039;s question, you should look into using try and catch in C++.  Visual C++ exception handling Try...Catch doesn&#039;t catch access violation exception? Catching access violation exceptions [...]</description>
		<content:encoded><![CDATA[<p>[...] handling?      To answer RobotGymnast&#8217;s question, you should look into using try and catch in C++.  Visual C++ exception handling Try&#8230;Catch doesn&#8217;t catch access violation exception? Catching access violation exceptions [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yongdong</title>
		<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/comment-page-2/#comment-93734</link>
		<dc:creator>Yongdong</dc:creator>
		<pubDate>Thu, 06 Aug 2009 09:49:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.thunderguy.com/plusplus/20050421/visual-c-exception-handling/#comment-93734</guid>
		<description>Good article about EH. To avoid the side effects with /EHa, we can write code as following in VC 2005 with /EHs:
void appGo()
{
  try
  {
     // application run....
  }
  catch(...)
  {
  }
}
void main()
{
  __try
  {
     appGo();
  }
  __except(1)
  {
    // do something
  }
We put all application code into appGo() function. Thus __try/__except can handle the win32 exception, and all c++ exception can be hanlded in appGo().

I dont know whether this method is practice.
Please correct me.</description>
		<content:encoded><![CDATA[<p>Good article about EH. To avoid the side effects with /EHa, we can write code as following in VC 2005 with /EHs:<br />
void appGo()<br />
{<br />
  try<br />
  {<br />
     // application run&#8230;.<br />
  }<br />
  catch(&#8230;)<br />
  {<br />
  }<br />
}<br />
void main()<br />
{<br />
  __try<br />
  {<br />
     appGo();<br />
  }<br />
  __except(1)<br />
  {<br />
    // do something<br />
  }<br />
We put all application code into appGo() function. Thus __try/__except can handle the win32 exception, and all c++ exception can be hanlded in appGo().</p>
<p>I dont know whether this method is practice.<br />
Please correct me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger</title>
		<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/comment-page-2/#comment-92286</link>
		<dc:creator>Roger</dc:creator>
		<pubDate>Wed, 01 Jul 2009 21:17:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.thunderguy.com/plusplus/20050421/visual-c-exception-handling/#comment-92286</guid>
		<description>This is one of the most informative articles I have seen on Exception Handling. It is definitly a must read for anyone who wants to know how to handle a Win32 exception in C++.</description>
		<content:encoded><![CDATA[<p>This is one of the most informative articles I have seen on Exception Handling. It is definitly a must read for anyone who wants to know how to handle a Win32 exception in C++.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Websites tagged "longjmp" on Postsaver</title>
		<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/comment-page-2/#comment-90161</link>
		<dc:creator>Websites tagged "longjmp" on Postsaver</dc:creator>
		<pubDate>Mon, 11 May 2009 02:02:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.thunderguy.com/plusplus/20050421/visual-c-exception-handling/#comment-90161</guid>
		<description>[...] - By: Tom Honermann saved by ProTrader2009-05-03 - C C++ Programmer Technical test questions saved by knowliz2009-04-27 [...]</description>
		<content:encoded><![CDATA[<p>[...] &#8211; By: Tom Honermann saved by ProTrader2009-05-03 &#8211; C C++ Programmer Technical test questions saved by knowliz2009-04-27 [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Try-catch works with structured exception &#124; keyongtech</title>
		<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/comment-page-2/#comment-82473</link>
		<dc:creator>Try-catch works with structured exception &#124; keyongtech</dc:creator>
		<pubDate>Sun, 18 Jan 2009 17:37:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.thunderguy.com/plusplus/20050421/visual-c-exception-handling/#comment-82473</guid>
		<description>[...] bug in MS&#039; exception handling. A catch(...) statement should only pick up things that are thrown.  http://www.thunderguy.com/semicolon/...tion-handling/   With VS2008 you can presumably select bug or no-bug with the proper compiler switches.   Bo [...]</description>
		<content:encoded><![CDATA[<p>[...] bug in MS&#8217; exception handling. A catch(&#8230;) statement should only pick up things that are thrown.  <a href="http://www.thunderguy.com/semicolon/...tion-handling/" rel="nofollow">http://www.thunderguy.com/semicolon/&#8230;tion-handling/</a>   With VS2008 you can presumably select bug or no-bug with the proper compiler switches.   Bo [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Honermann</title>
		<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/comment-page-2/#comment-77491</link>
		<dc:creator>Tom Honermann</dc:creator>
		<pubDate>Fri, 12 Sep 2008 06:27:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.thunderguy.com/plusplus/20050421/visual-c-exception-handling/#comment-77491</guid>
		<description>Hi Bennett,

I missed that you did mention that compilation with /EHa is required - sorry, my bad.

There are two negative side effects with the use of /EHa.  First, the overhead of the asynchronous unwind code affects performance and generates larger binaries.  Second, compilation with /EHa causes catch(...) handlers to be entered for Win32 exceptions.  Your post states that &quot;This problem does not occur in Visual C++ 2005, where catch(...) never catches Win32 exceptions.&quot;.  This isn&#039;t correct as the following code demonstrates:

#include 

int main()
{
    try
        {
        volatile int* pi = NULL;
        volatile int i = *pi;
        }
    catch(...)
        {
        std::cerr &lt;&lt; &quot;Caught exception&quot; &lt;&lt; std::endl;
        }
    return 0;
}

Compile this with &quot;cl /O2 /EHa /Zi /MD main.cpp /Femain.exe&quot; and run main.exe and you&#039;ll see that the access violation does cause the catch(...) handler to be invoked.

For all the reasons enumerated in your excellent post, this is disastrous for code quality and in my opinion sufficient reason to not compile with /EHa.

Unfortunately, I was mistaken in my previous comment regarding async-exception safe setjmp/longjmp.  I have since discovered that neither the &quot;setjmp.h&quot; or &quot;setjmpex.h&quot; setjmp/longjmp implementations are async-exception safe since both attempt to unwind the stack.  The Microsoft documentation is confused about this - see http://msdn.microsoft.com/en-us/library/yz2ez4as.aspx.  This documentation currently contradicts itself with regard to whether or not C++ stack objects are destroyed when longjmp is called.  In our environment (compiling with /EHsc), we have seen crashes with call stacks like this:

msvcr80!raise+0x187
msvcr80!abort+0x3d
msvcr80!terminate+0x4d
msvcr80!_inconsistency+0x22
msvcr80!__FrameUnwindToState+0x4b
msvcr80!__CxxLongjmpUnwind+0x14
msvcr80!longjmp+0x76

The fact that longjmp called a function named CxxLongjmpUnwind suggests that it does in fact destroy C++ stack objects while unwinding.

This has left me in a bind.  We need to do something like what you are suggesting - return to a previous execution point when a Win32 exception occurs, but compiling with /EHa is too burdensome since it will cause uses of catch(...) to catch Win32 exceptions.  I had turned to setjmp/longjmp hoping to solve the problem that way (I don&#039;t care about C++ stack objects being destroyed when returning to the previous execution point).  Unfortunately, I&#039;m probably going to have to implement my own setjmp/longjmp routines to do this safely without performing C++ stack unwinding.

Ideally, Microsoft would provide versions of setjmp/longjmp that do not attempt to perform C++ stack unwinding - similar to how the Single UNIX Spec provides sigsetjmp/siglongjmp to handle asynchronous signals.</description>
		<content:encoded><![CDATA[<p>Hi Bennett,</p>
<p>I missed that you did mention that compilation with /EHa is required &#8211; sorry, my bad.</p>
<p>There are two negative side effects with the use of /EHa.  First, the overhead of the asynchronous unwind code affects performance and generates larger binaries.  Second, compilation with /EHa causes catch(&#8230;) handlers to be entered for Win32 exceptions.  Your post states that &#8220;This problem does not occur in Visual C++ 2005, where catch(&#8230;) never catches Win32 exceptions.&#8221;.  This isn&#8217;t correct as the following code demonstrates:</p>
<p>#include </p>
<p>int main()<br />
{<br />
    try<br />
        {<br />
        volatile int* pi = NULL;<br />
        volatile int i = *pi;<br />
        }<br />
    catch(&#8230;)<br />
        {<br />
        std::cerr &lt;&lt; &#8220;Caught exception&#8221; &lt;&lt; std::endl;<br />
        }<br />
    return 0;<br />
}</p>
<p>Compile this with &#8220;cl /O2 /EHa /Zi /MD main.cpp /Femain.exe&#8221; and run main.exe and you&#8217;ll see that the access violation does cause the catch(&#8230;) handler to be invoked.</p>
<p>For all the reasons enumerated in your excellent post, this is disastrous for code quality and in my opinion sufficient reason to not compile with /EHa.</p>
<p>Unfortunately, I was mistaken in my previous comment regarding async-exception safe setjmp/longjmp.  I have since discovered that neither the &#8220;setjmp.h&#8221; or &#8220;setjmpex.h&#8221; setjmp/longjmp implementations are async-exception safe since both attempt to unwind the stack.  The Microsoft documentation is confused about this &#8211; see <a href="http://msdn.microsoft.com/en-us/library/yz2ez4as.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/yz2ez4as.aspx</a>.  This documentation currently contradicts itself with regard to whether or not C++ stack objects are destroyed when longjmp is called.  In our environment (compiling with /EHsc), we have seen crashes with call stacks like this:</p>
<p>msvcr80!raise+0&#215;187<br />
msvcr80!abort+0x3d<br />
msvcr80!terminate+0x4d<br />
msvcr80!_inconsistency+0&#215;22<br />
msvcr80!__FrameUnwindToState+0x4b<br />
msvcr80!__CxxLongjmpUnwind+0&#215;14<br />
msvcr80!longjmp+0&#215;76</p>
<p>The fact that longjmp called a function named CxxLongjmpUnwind suggests that it does in fact destroy C++ stack objects while unwinding.</p>
<p>This has left me in a bind.  We need to do something like what you are suggesting &#8211; return to a previous execution point when a Win32 exception occurs, but compiling with /EHa is too burdensome since it will cause uses of catch(&#8230;) to catch Win32 exceptions.  I had turned to setjmp/longjmp hoping to solve the problem that way (I don&#8217;t care about C++ stack objects being destroyed when returning to the previous execution point).  Unfortunately, I&#8217;m probably going to have to implement my own setjmp/longjmp routines to do this safely without performing C++ stack unwinding.</p>
<p>Ideally, Microsoft would provide versions of setjmp/longjmp that do not attempt to perform C++ stack unwinding &#8211; similar to how the Single UNIX Spec provides sigsetjmp/siglongjmp to handle asynchronous signals.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bennett</title>
		<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/comment-page-2/#comment-76321</link>
		<dc:creator>Bennett</dc:creator>
		<pubDate>Sat, 23 Aug 2008 11:14:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.thunderguy.com/plusplus/20050421/visual-c-exception-handling/#comment-76321</guid>
		<description>Thanks for your comment Tom. Just in case it wasn&#039;t clear, I agree that _set_se_translator does not play well with /EHs. That&#039;s why I said:

&lt;blockquote&gt;You must also compile with the /EHa compiler option to enable asynchronous exception handling. At runtime, you install your handler by calling the global function _set_se_translator().&lt;/blockquote&gt;

The dire consequences you describe can indeed happen in code compiled with /EHs. But using /EHa pretty much solves all these problems. Following the approach I describe in the article should yield well-behaved and useful C++ exceptions. I&#039;d like to know about any problems with this approach.</description>
		<content:encoded><![CDATA[<p>Thanks for your comment Tom. Just in case it wasn&#8217;t clear, I agree that _set_se_translator does not play well with /EHs. That&#8217;s why I said:</p>
<blockquote><p>You must also compile with the /EHa compiler option to enable asynchronous exception handling. At runtime, you install your handler by calling the global function _set_se_translator().</p></blockquote>
<p>The dire consequences you describe can indeed happen in code compiled with /EHs. But using /EHa pretty much solves all these problems. Following the approach I describe in the article should yield well-behaved and useful C++ exceptions. I&#8217;d like to know about any problems with this approach.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Honermann</title>
		<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/comment-page-2/#comment-76256</link>
		<dc:creator>Tom Honermann</dc:creator>
		<pubDate>Fri, 22 Aug 2008 21:47:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.thunderguy.com/plusplus/20050421/visual-c-exception-handling/#comment-76256</guid>
		<description>The proposed solution to throw a C++ exception from the _set_se_translator registered handler is not a safe operation when compiling with /EHs - occasionally, you&#039;ll get crashes in the stack unwinding code because of this.  The reason is that all the instructions necessary for properly unwinding the stack at any given execution point are omitted when compiling with /EHs.  Additionally, when compiling with optimizations, the compiler may elide C++ catch handlers that it determines cannot be entered (the compiler cannot infer that a Win32 exception may invoke a _set_se_translator registered handler that may throw a C++ exception) - which means the exception may not get caught where you expected it to.

At the point that a Win32 exception occurs, the stack and objects on the stack may not be in a consistent state.  Without the instructions added when compiling with /EHa, stack unwinding may result in destructors being called for partially constructed/destructed objects - which is a very bad thing.  Additionally, partially constructed objects may not be unwound properly (or at all) leading to resource leaks.

Win32 exceptions and exception handlers have all the same challenges as signals and signal handlers have on POSIX platforms.  I once read about someone advocating throwing C++ exceptions from signal handlers - which is a bad idea for exactly the same reasons as described above.

I recommend that catching of Win32 exceptions be limited to regions of code which do not require stack unwinding (ie, catch the exception in the stack frame that generated it and don&#039;t place any C++ objects in the try/catch block).  Or, use the async-exception safe longjmp/setjmp APIs (the ones in setjmp.h, NOT setjmpex.h).  These longjmp/setjmp APIs will not call destructors for objects on the stack, but will ensure that the stack is unwound safely.  Unfortunately, MS developers messed this up by not providing distinct interfaces to the async-exception safe setjmp/longjmp APIs (On POSIX systems, sigsetjmp and siglongjmp exist for this exact reason).  You need to be very careful to ensure that setjmpex.h was not included prior to setjmp.h</description>
		<content:encoded><![CDATA[<p>The proposed solution to throw a C++ exception from the _set_se_translator registered handler is not a safe operation when compiling with /EHs &#8211; occasionally, you&#8217;ll get crashes in the stack unwinding code because of this.  The reason is that all the instructions necessary for properly unwinding the stack at any given execution point are omitted when compiling with /EHs.  Additionally, when compiling with optimizations, the compiler may elide C++ catch handlers that it determines cannot be entered (the compiler cannot infer that a Win32 exception may invoke a _set_se_translator registered handler that may throw a C++ exception) &#8211; which means the exception may not get caught where you expected it to.</p>
<p>At the point that a Win32 exception occurs, the stack and objects on the stack may not be in a consistent state.  Without the instructions added when compiling with /EHa, stack unwinding may result in destructors being called for partially constructed/destructed objects &#8211; which is a very bad thing.  Additionally, partially constructed objects may not be unwound properly (or at all) leading to resource leaks.</p>
<p>Win32 exceptions and exception handlers have all the same challenges as signals and signal handlers have on POSIX platforms.  I once read about someone advocating throwing C++ exceptions from signal handlers &#8211; which is a bad idea for exactly the same reasons as described above.</p>
<p>I recommend that catching of Win32 exceptions be limited to regions of code which do not require stack unwinding (ie, catch the exception in the stack frame that generated it and don&#8217;t place any C++ objects in the try/catch block).  Or, use the async-exception safe longjmp/setjmp APIs (the ones in setjmp.h, NOT setjmpex.h).  These longjmp/setjmp APIs will not call destructors for objects on the stack, but will ensure that the stack is unwound safely.  Unfortunately, MS developers messed this up by not providing distinct interfaces to the async-exception safe setjmp/longjmp APIs (On POSIX systems, sigsetjmp and siglongjmp exist for this exact reason).  You need to be very careful to ensure that setjmpex.h was not included prior to setjmp.h</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bennett</title>
		<link>http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/comment-page-2/#comment-75325</link>
		<dc:creator>Bennett</dc:creator>
		<pubDate>Wed, 06 Aug 2008 08:53:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.thunderguy.com/plusplus/20050421/visual-c-exception-handling/#comment-75325</guid>
		<description>Thomas, it would be safest to build everything with /EHa unless you have some specific reason you need to disable it.</description>
		<content:encoded><![CDATA[<p>Thomas, it would be safest to build everything with /EHa unless you have some specific reason you need to disable it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

