<?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>try/catch and scoping</title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx</link><description>I received an interesting customer question last week, and thought it would be valuable to share the answer here. Here's the question: &amp;lt;question&amp;gt; On the subject of try/catch scope in C#, it would be useful if the scope of variables created in try</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>try/catch and scoping </title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx#403269</link><pubDate>Tue, 29 Mar 2005 14:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:403269</guid><dc:creator>Rudolf Henning says...</dc:creator><description>&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=403269" width="1" height="1"&gt;</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx#401126</link><pubDate>Wed, 23 Mar 2005 19:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:401126</guid><dc:creator>Michael Olivero</dc:creator><description>My solution to the problem is:&lt;br&gt;&lt;br&gt;{ //extra dummy scope for the anomaly&lt;br&gt;&lt;br&gt;object o = null;  //decare in dummy scope&lt;br&gt;&lt;br&gt;try {&lt;br&gt;  object o = new object();&lt;br&gt;}&lt;br&gt;catch {&lt;br&gt;  Console.WriteLine(o);&lt;br&gt;}&lt;br&gt;&lt;br&gt;}&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=401126" width="1" height="1"&gt;</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx#400786</link><pubDate>Wed, 23 Mar 2005 05:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400786</guid><dc:creator>Don Newman</dc:creator><description>Wouldn't an easy workaround be:&lt;br&gt;&lt;br&gt;try&lt;br&gt;{&lt;br&gt;object o = null;&lt;br&gt;try&lt;br&gt;{&lt;br&gt;o = new object();&lt;br&gt;}&lt;br&gt;catch&lt;br&gt;{&lt;br&gt;Console.WriteLine(o);&lt;br&gt;}&lt;br&gt;}&lt;br&gt;// object o is now out of scope&lt;br&gt;catch&lt;br&gt;{&lt;br&gt;Console.WriteLine(ErrorMessage)&lt;br&gt;}&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=400786" width="1" height="1"&gt;</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx#400608</link><pubDate>Tue, 22 Mar 2005 20:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400608</guid><dc:creator>Thomas Eyde</dc:creator><description>If it's so important to narrow down the accessibility to a variable, why don't you just extract the code which need it to its own method?&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=400608" width="1" height="1"&gt;</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx#400411</link><pubDate>Tue, 22 Mar 2005 15:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400411</guid><dc:creator>Scott Wisniewski</dc:creator><description>Instituting that change would cause try / catch blocks to use fundamentally differnt scoping rules than the rest of the language.&lt;br&gt;I think thats dangerous. One of the nice things about C# and the .NET framework is that they are relative simple, consistent, and have few suprises. &lt;br&gt;&lt;br&gt;Violating that, just for the benefit of not having to declare varaibles outside of a try block, would be a big mistake.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=400411" width="1" height="1"&gt;</description></item><item><title>Interesting post about try/catch scope</title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx#400347</link><pubDate>Tue, 22 Mar 2005 14:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400347</guid><dc:creator>Changing the way people think</dc:creator><description>&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=400347" width="1" height="1"&gt;</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx#400351</link><pubDate>Tue, 22 Mar 2005 12:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400351</guid><dc:creator>Douglas Reilly</dc:creator><description>By the way, a variation on the theme of what GC will collect, in debug mode, GC will not collect an object until it is out of scope, allowing you to debug and watch all variables that are actually in scope, even if code will not touch it.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=400351" width="1" height="1"&gt;</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx#400341</link><pubDate>Tue, 22 Mar 2005 11:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400341</guid><dc:creator>Chris Nahr</dc:creator><description>I stand corrected. Thanks for the link, that's quite surprising. In that case I agree with your example and argument.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=400341" width="1" height="1"&gt;</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx#400327</link><pubDate>Tue, 22 Mar 2005 10:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400327</guid><dc:creator>David M. Kean</dc:creator><description>Chris,&lt;br&gt;&lt;br&gt;You are incorrect. The GC does not rely on scopes to determine if an object is referenced anymore.&lt;br&gt;&lt;br&gt;See this post by Chris Brumme:&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://blogs.msdn.com/cbrumme/archive/2003/04/19/51365.aspx"&gt;http://blogs.msdn.com/cbrumme/archive/2003/04/19/51365.aspx&lt;/a&gt;.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=400327" width="1" height="1"&gt;</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/b/scottwil/archive/2005/03/21/400140.aspx#400283</link><pubDate>Tue, 22 Mar 2005 07:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400283</guid><dc:creator>Chris Nahr</dc:creator><description>David: The GC cannot detect an object not being used, it can only detect objects that aren't being referenced anymore. The reference must either go out of scope (which is what the proposal was about) or you must manually set it to a null reference. Your example wouldn't work.&lt;br&gt;&lt;br&gt;That said, I still think the proposed idea is a bad one because it breaks a fundamental rule for lifetime scoping in C-based languages: dynamic variables live from &amp;quot;{&amp;quot; to the matching &amp;quot;}&amp;quot;, and no further.&lt;br&gt;&lt;br&gt;If you have two consecutive (non-nested) {} blocks, dynamic variables declared in the first block shouldn't survive to see the other block. Exceptions to this rule are too confusing to the reader.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=400283" width="1" height="1"&gt;</description></item></channel></rss>