<?xml version="1.0" encoding="UTF-8"?><item><title>C# language annoyance of the day</title><description>&lt;DIV&gt;This actually applies to all the C-derived languages.&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&#13;
&lt;DIV&gt;If I have code like this:&lt;/DIV&gt;&#13;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;&#13;
&lt;DIV&gt;File f = File.OpenText(&amp;#8220;MyFile.txt&amp;#8221;);&lt;/DIV&gt;&#13;
&lt;DIV&gt;f.ReadLine();&lt;/DIV&gt;&lt;/BLOCKQUOTE&gt;&#13;
&lt;DIV&gt;And I want to handle the case where the file is not found:&lt;/DIV&gt;&#13;
&lt;DIV&gt;&#13;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;&#13;
&lt;DIV&gt;try&lt;/DIV&gt;&#13;
&lt;DIV&gt;{ &lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp; File f = File.OpenText(&amp;#8220;MyFile.txt&amp;#8221;);&lt;/DIV&gt;&#13;
&lt;DIV&gt;} catch (&lt;A href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cpref/html/frlrfsystemiofilenotfoundexceptionclasstopic.htm"&gt;FileNotFoundException&lt;/A&gt;&amp;nbsp;ex)&lt;/DIV&gt;&#13;
&lt;DIV&gt;{&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp; Console.WriteLine(&amp;#8220;File not found.&amp;#8221;);&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp; return false;&lt;/DIV&gt;&#13;
&lt;DIV&gt;}&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&#13;
&lt;DIV&gt;f.ReadLine();&lt;/DIV&gt;&lt;/BLOCKQUOTE&gt;&lt;/DIV&gt;&#13;
&lt;DIV&gt;The problem with this is that the scope of f ends at the end of the try block.&amp;nbsp; To get around this, I'd have to declare f outside the try/catch:&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&#13;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;&#13;
&lt;DIV&gt;File f;&lt;/DIV&gt;&#13;
&lt;DIV&gt;try&lt;/DIV&gt;&#13;
&lt;DIV&gt;{ &lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp; f = File.OpenText(&amp;#8220;MyFile.txt&amp;#8221;);&lt;/DIV&gt;&#13;
&lt;DIV&gt;} catch (&lt;A href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cpref/html/frlrfsystemiofilenotfoundexceptionclasstopic.htm"&gt;FileNotFoundException&lt;/A&gt;&amp;nbsp;ex)&lt;/DIV&gt;&#13;
&lt;DIV&gt;{&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp; Console.WriteLine(&amp;#8220;File not found.&amp;#8221;);&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp; return false;&lt;/DIV&gt;&#13;
&lt;DIV&gt;}&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&#13;
&lt;DIV&gt;f.ReadLine();&lt;/DIV&gt;&lt;/BLOCKQUOTE&gt;&#13;
&lt;DIV dir=ltr&gt;&#13;
&lt;DIV&gt;This seems less than elegant to me.&amp;nbsp; What I'd like to see is either variables living beyond the scope of a try/catch, or a way to mark a variable declared inside a block as being visible in the parent block.&amp;nbsp; For example:&lt;/DIV&gt;&#13;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;&#13;
&lt;DIV&gt;&#13;
&lt;DIV&gt;try&lt;/DIV&gt;&#13;
&lt;DIV&gt;{ &lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp; File parent.f = File.OpenText(&amp;#8220;MyFile.txt&amp;#8221;);&lt;/DIV&gt;&#13;
&lt;DIV&gt;} catch (&lt;A href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cpref/html/frlrfsystemiofilenotfoundexceptionclasstopic.htm"&gt;FileNotFoundException&lt;/A&gt;&amp;nbsp;ex)&lt;/DIV&gt;&#13;
&lt;DIV&gt;{&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp; Console.WriteLine(&amp;#8220;File not found.&amp;#8221;);&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp; return false;&lt;/DIV&gt;&#13;
&lt;DIV&gt;}&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&#13;
&lt;DIV&gt;f.ReadLine();&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BLOCKQUOTE&gt;&#13;
&lt;DIV&gt;The syntax doesn't matter that much as long as it's clear; it's the convenience of not having to go and separately declare and initialize all the variables you're using inside the try/catch that would be useful.&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&#13;
&lt;DIV&gt;Why not just do the f.ReadLine inside the try/catch?&amp;nbsp; Because if this is a large function (imagine the f.ReadLine is actually&amp;nbsp;80 lines of code), the&amp;nbsp;distance between where the error is detected and where the error is handled can be quite large and you lose the ability to see 'at a glance' if the error is handled, and how.&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&#13;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description><pubDate>Thu, 01 Sep 2005 19:17:07 GMT</pubDate></item>