<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : TechTip: DebuggerNonUserCode</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce Classic : TechTip: DebuggerNonUserCode</description>
  <pubDate>Fri, 12 Jun 2026 08:39:43 -700</pubDate>
  <lastBuildDate>Mon, 09 Jul 2007 12:17:13 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=117</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>TechTip: DebuggerNonUserCode :        Level 200 DevForce...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=117&amp;PID=255#255</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=21" rel="nofollow">IdeaBlade</a><br /><strong>Subject:</strong> 117<br /><strong>Posted:</strong> 09-Jul-2007 at 12:17pm<br /><br /><TABLE id=c&#111;ntent_LETTER.BLOCK3 hideFocus style="MARGIN-BOTTOM: 10px" =0 cellSpacing=0 cellPadding=5 width="100%"><T><TR><TD style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Verdana,Geneva,Arial,Helvetica,sans-serif"><FONT style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Verdana,Geneva,Arial,Helvetica,sans-serif" face=Verdana,Geneva,Arial,Helvetica,sans-serif color=#000000 size=2><TABLE cellSpacing=0 cellPadding=7 width="100%"><T><TR vAlign=top =#ffffff><TD width=454 height=18><a href="http://rs6.net/tn.jsp?t=qlwc86bab.0.lirg6ubab.zufnlmbab.2355&amp;ts=S0234&amp;p=http%3A%2F%2Fwww.ideablade.com%2Ftech_tips_definiti&#111;ns.htm" target="_blank"><FONT title=http://rs6.net/tn.jsp?t=qlwc86bab.0.lirg6ubab.zufnlmbab.2355&amp;ts=S0234&amp;p=http%3A%2F%2Fwww.ideablade.com%2Ftech_tips_definiti&#111;ns.htm face=Arial,Helvetica,sans-serif size=1>Level 200 <BR title=http://rs6.net/tn.jsp?t=qlwc86bab.0.lirg6ubab.zufnlmbab.2355&amp;ts=S0234&amp;p=http%3A%2F%2Fwww.ideablade.com%2Ftech_tips_definiti&#111;ns.htm>DevForce Express</FONT></A></TD><TD width=158><DIV align=center><a href="http://rs6.net/tn.jsp?t=qlwc86bab.0.nblx5vbab.zufnlmbab.2355&amp;ts=S0234&amp;p=http%3A%2F%2Fwww.ideablade.com%2Ftechtips_summary.html" target="_blank"><FONT title=http://rs6.net/tn.jsp?t=qlwc86bab.0.nblx5vbab.zufnlmbab.2355&amp;ts=S0234&amp;p=http%3A%2F%2Fwww.ideablade.com%2Ftechtips_summary. face=Arial,Helvetica,sans-serif size=1>View Archived Tech Tips</FONT></A></DIV></TD></TR><TR vAlign=top =#ffffff><TD colSpan=2 height=18><TABLE cellSpacing=0 cellPadding=0 width="100%"><T><TR><TD width="8%"><img src="http://www.ideablade.com/images/lightbulb.jpg" height="33" width="33" border="0" /></TD><TD width="92%"><strong><FONT face=Arial,Helvetica,sans-serif size=2>DebuggerNonUserCode</FONT></strong></TD></TR></T></TABLE></TD></TR></T></TABLE></FONT></TD></TR></T></TABLE><TABLE id=c&#111;ntent_LETTER.BLOCK4 width="100%"><T><TR><TD><TABLE cellSpacing=0 cellPadding=0 width="100%"><T><TR vAlign=top><TD colSpan=2><P><FONT face=Arial,Helvetica,sans-serif size=2>Visual Studio 2005 has some exceptional debugging capabilities, but every so often we encounter a situation where we get some very non-intuitive debugging behaviors. Among the more annoying of these is when we are running our code in the debugger and it breaks at some strange location in some third-party vendor's code. We know that this is not our code so our first assumption is that we have somehow abused the <BR>third-party's API. or that the third party has some bug in their code. </FONT></P><P><FONT face=Arial,Helvetica,sans-serif size=2>Occasionally, neither of these is true. Sometimes in these cases after encountering the exception we tell the debugger to continue, without changing anything, and the code continues to run and behaves exactly as intended. This same code also runs without a problem in release mode. So, why the exception? Is there some hidden problem in the calling code, or in the third-party library? </FONT></P><P><FONT face=Arial,Helvetica,sans-serif size=2>This condition is usually caused by the combination of code generators or sample code that is embedded into an application along with the generated code's assumption that any exception that it throws will be handled properly further up the stack. </FONT></P><P><FONT face=Arial,Helvetica,sans-serif size=2>Within IdeaBlade we encounter this situation within our auto-generated property setter code. The example below is a simplified version of IdeaBlade property setter code. Inside this auto-generated setter logic, IdeaBlade performs a number of business validation checks and will throw a specific exception if any of these checks fail. Ideally we want this code to break at the location where the LastName setter was called. Unfortunately, the code as shown below will instead break inside of the setter.</FONT></P><DIV>&nbsp;</DIV></TD></TR><TR><TD width="6%">&nbsp;</TD><TD width="94%"><P><FONT face=Arial,Helvetica,sans-serif size=2><strong>C#:</strong></FONT></P><P><FONT face="Courier New,Courier,Monospace" color=#0000ff size=2>public virtual</FONT><FONT face="Courier New,Courier,Monospace" color=#000000 size=2> System.String LastName {<BR>&nbsp;&nbsp;<FONT color=#0000ff>get</FONT> { . . . }<BR><BR>&nbsp;&nbsp;<FONT color=#0000ff>set</FONT> {<BR>&nbsp;&nbsp;&nbsp;&nbsp;// SetValue throws a Validation exception if the<BR>&nbsp;&nbsp;&nbsp;&nbsp;// "value" provided does not meet prespecified <BR>&nbsp;&nbsp;&nbsp;&nbsp;// business rules. <BR>&nbsp;&nbsp;&nbsp;&nbsp;SetValue("LastName", <FONT color=#0000ff>value</FONT>);<BR>&nbsp;&nbsp;}<BR>}<BR></FONT></P><P><BR><FONT face=Arial,Helvetica,sans-serif><FONT size=2><strong>VB.NET:</strong> </FONT></FONT></P><P><FONT face="Courier New,Courier,Monospace" color=#0000ff size=2>Public Overridable Property</FONT><FONT color=#000000><FONT face="Courier New,Courier,Monospace" size=2> LastName() <FONT color=#0000ff>As</FONT> System.String<BR><FONT color=#0000ff>&nbsp;&nbsp;Get</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... <BR></FONT><FONT face="Courier New,Courier,Monospace"><FONT size=2><FONT color=#0000ff>&nbsp;&nbsp;End Get<BR><BR>&nbsp;&nbsp;Set</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;' SetValue throws a Validation exception if the<BR>&nbsp;&nbsp;&nbsp;&nbsp;' "value" provided does not meet prespecified<BR>&nbsp;&nbsp;&nbsp;&nbsp;' business rules.<BR>&nbsp;&nbsp;&nbsp;&nbsp;SetValue("LastName", Value)<BR></FONT></FONT><FONT face="Courier New,Courier,Monospace" color=#0000ff size=2>&nbsp;&nbsp;End Set<BR>End Property</FONT></FONT></P></TD></TR></T></TABLE></TD></TR></T></TABLE><TABLE id=c&#111;ntent_LETTER.BLOCK5 width="100%"><T><TR><TD><TABLE cellSpacing=0 cellPadding=0 width="100%"><T><TR><TD colSpan=2><P><FONT face=Arial,Helvetica,sans-serif size=2>From the standpoint of the developer debugging this application, this is fairly non-intuitive. I want to know where the code that I wrote broke, not where the auto-generated code failed. This is a fairly simple case but in the real world the stack is often much deeper and consequently even harder to get a handle on.</FONT></P><P><FONT face=Arial,Helvetica,sans-serif><FONT size=2>The fix is fairly simple. The code below causes the debugger to break on the line that called the LastName setter <strong>unless that code is itself inside of a try catch block</strong>. What is interesting is that these two code samples cause very different behaviors when run inside the debugger, but identical behavior when run outside of it.</FONT></FONT></P><DIV>&nbsp;</DIV></TD></TR><TR><TD width="6%"><FONT face=Arial,Helvetica,sans-serif size=2>&nbsp;</FONT></TD><TD width="94%"><P><strong><FONT face=Arial,Helvetica,sans-serif size=2>C#:</FONT></strong></P><P><FONT face="Courier New,Courier,Monospace"><FONT size=2><strong>&#091;System.Diagnostics</strong>.<strong><FONT color=#006666>DebuggerNonUserCode</FONT></strong>&#093;<BR><FONT color=#0000ff>public virtual</FONT> System.<FONT color=#006666>String</FONT> LastName {<BR>&nbsp;&nbsp;<FONT color=#0000ff>get</FONT> { . . . }<BR><BR>&nbsp;&nbsp;<FONT color=#0000ff>set</FONT> {<BR>&nbsp;&nbsp;&nbsp;&nbsp;// SetValue throws a Validation exception if the<BR>&nbsp;&nbsp;&nbsp;&nbsp;// "value" provided does not meet prespecified<BR>&nbsp;&nbsp;&nbsp;&nbsp;// business rules.<BR>&nbsp;&nbsp;&nbsp; SetValue("LastName", <FONT color=#0000ff>value</FONT>);<BR>&nbsp;&nbsp;}<BR>} </FONT></FONT></P><P><strong><FONT face=Arial,Helvetica,sans-serif size=2>VB.NET:</FONT></strong></P><FONT face="Courier New,Courier,Monospace"><FONT size=2><strong>&lt;</strong><FONT color=#000000>System.Diagnostics.DebuggerNonUserCode</FONT>&gt; _<BR><FONT color=#0000ff>Public Overridable Property</FONT> LastName() <FONT color=#0000ff>As</FONT> System.String<BR>&nbsp;&nbsp;<FONT color=#0000ff>Get</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;. . .<BR><FONT color=#0000ff>&nbsp;&nbsp;End Get</FONT><BR><BR>&nbsp;&nbsp;<FONT color=#0000ff>Set</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;' SetValue throws a Validation exception if the<BR>&nbsp;&nbsp;&nbsp;&nbsp;'"value" provided does not meet prespecified<BR>&nbsp;&nbsp;&nbsp;&nbsp;' business rules.<BR>&nbsp;&nbsp;&nbsp;&nbsp;SetValue("LastName", <FONT color=#000000>Value</FONT>)<BR><FONT color=#0000ff>&nbsp;&nbsp;End Set</FONT><BR><FONT color=#0000ff>End Property</FONT></FONT></FONT> </TD></TR></T></TABLE></TD></TR></T></TABLE><TABLE id=c&#111;ntent_LETTER.BLOCK6 width="100%"><T><TR><TD><TABLE cellSpacing=0 cellPadding=0 width="100%"><T><TR><TD><P><FONT face=Arial,Helvetica,sans-serif size=2>It turns out that, in practice, code like that shown above is often executed in the context of a WinForms application where the LastName property is bound to some control via .NET databinding. In this case, .NET databinding is built with the expectation that errors such as this may be thrown inside of a property setter and it that the databinding infrastructure will handle the exception. This means that we really do <strong>not</strong> want the debugger to break on the exception. We want the .NET databinding apparatus to handle this exception so that we in turn can use the databinding mechanisms to provide an error bullet or some other UI manifestation of the error. </FONT></P><P><FONT face=Arial,Helvetica,sans-serif size=2>So why is any of this important to non-framework developers?</FONT></P><P><FONT face=Arial,Helvetica,sans-serif size=2>The reason is that the case described above is not limited to third-party framework code. In fact we often find within large applications the need to have lower levels in the application stack throw errors that we "expect" to catch and handle further up the stack. Each of these is a candidate for the <FONT color=#006666><strong>DebuggerNonUserCodeAttribute</strong></FONT>.</FONT></P><P><FONT face=Arial,Helvetica,sans-serif size=2>So what if we want the best of both worlds? What if we sometimes want to debug this at the lowest possible level and sometimes we want to debug at a higher "intent" level?</FONT></P><P><FONT face=Arial,Helvetica,sans-serif size=2>Visual Studio provides a toggle to do exactly this. Inside of the <strong>Tools - Options - Debugging</strong> window there is a checkbox next to the option "Enable Just My Code". By default, this is checked and the debugger will behave as described above. However, by unchecking this option, we in effect remove the effect of the <FONT color=#006666><strong>DebuggerNonUserCodeAttribute</strong></FONT> and revert back to our original behavior.</FONT></P><P><FONT face=Arial,Helvetica,sans-serif size=2>There is also a <FONT color=#006666><strong>DebuggerHiddenAttribute</strong></FONT> which hides the code from the debugger, even if "Enable Just My Code" is turned off. This is intended for those cases where we never want to let the developer debug into the specified method or property at all. In effect, this constitutes a more absolute version of the <FONT color=#006666><strong>DebuggerNonUserCodeAttribute</strong></FONT>.</FONT></P></TD></TR></T></TABLE></TD></TR></T></TABLE>]]>
   </description>
   <pubDate>Mon, 09 Jul 2007 12:17:13 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=117&amp;PID=255#255</guid>
  </item> 
 </channel>
</rss>