<?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/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>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/scottwil/archive/2005/03/21/400140.aspx#400165</link><pubDate>Tue, 22 Mar 2005 02:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400165</guid><dc:creator>David M. Kean</dc:creator><description>&amp;gt; but this introduces side effects such as having the object around for longer than intended.&lt;br&gt;&lt;br&gt;This is a non-issue because the GC can detect when a object is not being used, so as long as you don't touch it after the catch, the GC could collect it anytime.&lt;br&gt;&lt;br&gt;For example:&lt;br&gt;&lt;br&gt;object o = null;&lt;br&gt;&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;// GC could collect 'o' here&lt;br&gt;&lt;br&gt;LongRunningOperation();&lt;br&gt;</description></item><item><title>Timing is everything</title><link>http://blogs.msdn.com/scottwil/archive/2005/03/21/400140.aspx#400176</link><pubDate>Tue, 22 Mar 2005 04:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400176</guid><dc:creator>Patrick Steele's .NET Blog</dc:creator><description /></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/scottwil/archive/2005/03/21/400140.aspx#400209</link><pubDate>Tue, 22 Mar 2005 04:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:400209</guid><dc:creator>Bill McCarthy</dc:creator><description>&amp;lt;quote&amp;gt;&lt;br&gt;The real reason is what Peter Hallam says: You can't assume that variables declared in the try block are definitely assigned on entry to the catch block (e.g. in your example, the &amp;quot;new object()&amp;quot; might throw an out of memory exception). Thus, we'd have to require separate initialization in the catch block, which would defeat the purpose of the suggestion.&lt;br&gt;&amp;lt;/quote&amp;gt;&lt;br&gt;&lt;br&gt;Seems to be missing the issue.  If you have to declare the object outside the Try block, you end up with no handlers for this situation</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/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.</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/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;.</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/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.</description></item><item><title>Interesting post about try/catch scope</title><link>http://blogs.msdn.com/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 /></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/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.</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/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.</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/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?</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/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;</description></item><item><title>re: try/catch and scoping</title><link>http://blogs.msdn.com/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;}</description></item><item><title>try/catch and scoping </title><link>http://blogs.msdn.com/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 /></item></channel></rss>