<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:osnews="http://osnews.com/rss2#">
	<channel>
		<title>OSNews: </title>
		<link>http://www.osnews.com/story/6856/Exception_Management_in_NET</link>
		<description>Exploring the Future of Computing</description>
		<language>en-us</language>
		<copyright>Copyright 2001-2009, David Adams</copyright>
		<webMaster>adam+nospam@osnews.com</webMaster>
		<lastBuildDate>Tue, 10 Nov 2009 09:21:49 GMT</lastBuildDate>
		<image>
			<url>http://www.osnews.com/images/osnews.gif</url>
			<title>OSNews.com</title>
			<link>http://www.osnews.com</link>
		</image>
		<item>
			<title>Don't forget about the finally block,s</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>(Arghh! It working in the preview! Any change of an intelligent  tag support, it'd be okay if it chopped excessively long lines)<br />
<br />
A powerful part of the exception mechanism is the finally block. This is ALWAYS executed, whether your program hits an error or not. Thus it is often used to free resources (dynamically allocated memory, database connections, network connections or files).<br />
<br />
e.g. (from Java)<br />
<br />
public static void copy(String[] args) <br />
{<br />
    File inputFile = null, outputFile = null;<br />
    try<br />
    {  inputFile = new File(&quot;farrago.txt&quot;);<br />
        outputFile = new File(&quot;outagain.txt&quot;);<br />
<br />
        FileReader in = new FileReader(inputFile);<br />
        FileWriter out = new FileWriter(outputFile);<br />
        int c;<br />
<br />
        while ((c = in.read()) != -1)<br />
            out.write(c);<br />
    }<br />
    catch (FileNotFoundException fnfe)<br />
    {      // do stuff<br />
    }<br />
    catch (IOException e)<br />
    {    // do stuff<br />
    }<br />
    catch (Exceptione e)<br />
    {    // do stuff<br />
    }<br />
    finally<br />
    {    // whether the program worked or threw an<br />
        // exception, we close the files<br />
        in.close();<br />
        out.close();<br />
    }<br />
}<br />
<br />
<br />
In this way you're guaranteed to close your files, no matter what happens. These finally blocks are incredibly useful, and are essential if you start doing socket or DB programming in modern OOP languages like Java or C#. They're also essential to avoid memory leaks if you're dealing with dynamic memory allocation like in C++ or Delphi.</description>
			<pubDate>Tue, 27 Apr 2004 08:43:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE:  Don't forget about the finally block,s</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>This is kinda embarrassing, but the finally block should be<br />
<br />
finally<br />
{   if (inputFile != null) inputFile.close();<br />
    if (outputFile != null) outputFile.close();<br />
}</description>
			<pubDate>Tue, 27 Apr 2004 09:37:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE: Don't forget about the finally block,s</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>So why can't you just put the close statements after the catch  blocks? They will be executed with or without exceptions.</description>
			<pubDate>Tue, 27 Apr 2004 10:09:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>@ So why can't you just put the close statements after the catch blocks?</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>in .Net, no code outside of a &quot;catch&quot; statement is guaranteed to run unless it's in a &quot;finally&quot; block.  Just comes w/ the framework, plus this lends itself to better coding practices.</description>
			<pubDate>Tue, 27 Apr 2004 10:26:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE: So why can't you just put the close</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>You may exit the method in a catch block, or you may quit the program in a catch block. Whatever you do though, the finally block will always get called.<br />
<br />
Also, as Jayson said, it's considered better programming practice, it makes it explicit what resources you're using and protects against other programmers altering, e.g. exception handlers, in the future.</description>
			<pubDate>Tue, 27 Apr 2004 10:38:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>exception this? try ST</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>exception in smalltalk are way better, cause they have #resum().<br />
This mean that you can restart the processing from the exact method instead of a spurious block. <br />
Even, exception's statement modifer are really useful, too bad common .net languages do not use them :/</description>
			<pubDate>Tue, 27 Apr 2004 12:15:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Re: exception this? try ST</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>Please let's not turn this into one more &quot;language war&quot; <img src="/images/emo/tongue.gif" alt=";)" /></description>
			<pubDate>Tue, 27 Apr 2004 12:36:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>finally block</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>in most instances, you have to put try/catch around close methods, because they throw IOException.  this, in java, is *the* correct way:<br />
<br />
FileReader in = null;<br />
try {<br />
in = new FileReader( &quot;filename&quot; );<br />
// do stuff<br />
} catch ( IOException e ) {<br />
// print error<br />
} catch ( FileNotFoundException e ) {<br />
// print error<br />
} finally {<br />
if ( in != null ) {<br />
try {<br />
in.close();<br />
} catch ( IOException e ) {<br />
// print file close error<br />
}<br />
}<br />
}</description>
			<pubDate>Tue, 27 Apr 2004 12:42:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>@rod</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>I did not meant to be rude, and I beg your pardon <img src="/images/emo/smile.gif" alt=";)" /> <br />
<br />
I just believe that Exception#resume() is a wonderful tool, and I'd like to see it in more environments.<br />
 <br />
BTW people may argue that they are not good, I just wonder why.<br />
<br />
Say, Ruby has first class reified continuations and it is quite easy to write a ResumableException class, yet this is not in the standard Exception's , maybe matz has an answer :/</description>
			<pubDate>Tue, 27 Apr 2004 13:09:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Exceptions vs. Error Codes</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>Only last week, I wrote about exceptions vs. error codes in my blog. I think a lot of times exception classes are created and exceptions thrown where only returning a status code would be the right thing to do.<br />
<br />
<a href="http://www.livejournal.com/~mannu/164080.html" rel="nofollow">http://www.livejournal.com/~mannu/164080.html</a></description>
			<pubDate>Tue, 27 Apr 2004 14:46:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>jeez, you guys</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>&gt;This is kinda embarrassing, but the finally block should be <br />
<br />
&gt;finally <br />
&gt;{ if (inputFile != null) inputFile.close(); <br />
&gt;if (outputFile != null) outputFile.close(); <br />
&gt;}<br />
<br />
Well, that works.  But this form is much cleaner:<br />
<br />
try{<br />
  inputFile = new File(&quot;farrago.txt&quot;); <br />
  outputFile = new File(&quot;outagain.txt&quot;); <br />
  try{<br />
  }finally{<br />
    inputFile.close();<br />
    outputFile.close();<br />
  }<br />
   <br />
catch(IOException e){<br />
blahblah<br />
}<br />
<br />
<br />
The key is you don't need all this if blah!=null business with the above approach.  When you open a file or resource, always code your finally at that point.  If an exception is throws on allocation/opening the  resource your variables will stay null and you will not enter the try-finally block.</description>
			<pubDate>Tue, 27 Apr 2004 14:53:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>good</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>Hmm a good article about Exception.</description>
			<pubDate>Tue, 27 Apr 2004 15:16:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Bryan: finally  doesn't always gets executed..</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>This is ALWAYS executed<br />
Well that's not true:<br />
try<br />
{<br />
//do something here<br />
catch(Exception e)<br />
{<br />
//unrecoverable error, we loose<br />
System.exit(1);<br />
}<br />
finally<br />
{<br />
// all resorces doesn't get cleaned<br />
System.out.println(&quot;This is not executed in case of an exception&quot;);<br />
}</description>
			<pubDate>Tue, 27 Apr 2004 16:17:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Re:jeez, you guys</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>yeah, thats called &quot;who guards the guards&quot; scheme..</description>
			<pubDate>Tue, 27 Apr 2004 16:20:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>What about using blocks?</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>I find it surprising that neither the article nor the comments have mentioned the using block yet.  It can greatly clean up code and simplify resource cleanup:<br />
<br />
using (FileStream input = File.OpenRead (&quot;input.txt&quot;)) {<br />
____using (FileStream output = File.OpenWrite (&quot;output.txt&quot;)) {<br />
________// copy file contents<br />
________byte[] buffer = new byte[256];<br />
________int nread;<br />
________while ((nread = input.Read (buffer, 0, buffer.Length)) != -1) {<br />
____________output.Write (buffer, 0, nread);<br />
________}<br />
____}<br />
}<br />
<br />
(My apologies for using underscores for spaces, but it's more legible than &amp;nbsp; or not providing any indentation...)<br />
<br />
No finally blocks, no worries about exceptions or resource cleanup, just plain code.  Of course, if an exception is generated the exception will escape this code block (unlike the original Java version, which would swallow all exceptions), but the resources will still be disposed of cleanly, whether or not an exception is generated.<br />
<br />
As in C++, the cleanest and most maintainable C# code doesn't use try/catch explicitly for resource cleanup.  C++ instead uses block-scoped objects with destructors, and C# offers the using block to simplify resource management.  It would be advisable to use it whenever possible, to help remove code errors and other embarassments.<br />
<br />
Finally, by implementing the System.IDisposable interface, you can implement your own resource cleanup strategies, which can be used for such esoteric things as locks with timeouts.<br />
<br />
See also:<br />
<a href="http://www.interact-sw.co.uk/iangblog/2004/03/23/locking" rel="nofollow">http://www.interact-sw.co.uk/iangblog/2004/03/23/locking</a> <br />
<a href="http://blogs.msdn.com/ericgu/archive/2004/03/28/100872.aspx" rel="nofollow">http://blogs.msdn.com/ericgu/archive/2004/03/28/100872.aspx</a></description>
			<pubDate>Tue, 27 Apr 2004 16:51:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE: phil</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>throwing null pointer exceptions is bad practice, nor should you nest the try in that way. i disagree. mainly because i, and my professors shun anything but that try/catch/finally/try/catch system</description>
			<pubDate>Tue, 27 Apr 2004 17:25:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Exceptionally poorly-written</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>&quot;(Note: This article won the OSNews Aspire competition a few weeks ago.)&quot;<br />
<br />
The poor writing and editing make me wonder what standards the OSNews Aspire competition adheres to. The technical content is lightweight enough, but the article is made much worse by horrible grammar and writing.<br />
<br />
The rules of English composition are no more a matter of opinion or &quot;style&quot; than Java syntax is. Programmers should put as much effort into communicating with other people and less into showing off their mastery of technology.</description>
			<pubDate>Tue, 27 Apr 2004 17:32:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Re: Exceptionally poorly-written</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>Did you say:<br />
&quot;[i]Programmers should put <b>as much</b> effort into communicating with other people and less into showing off their mastery of technology.[i]&quot;<br />
<br />
I'm sure a <b>master of grammar</b> like yourself meant to say:<br />
<br />
&quot;[i]Programmers should put <b>more</b> effort into communicating with other people and less into showing off their mastery of technology.[i]&quot;<br />
<br />
Have a nice day</description>
			<pubDate>Tue, 27 Apr 2004 17:38:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Re: Exceptionally poorly-written</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>&gt; but the article is made much worse by horrible grammar and writing. <br />
<br />
As we explain in the competition, grammar does not matter, content does. This way, non-english people can also participate to our competition, otherwise, we discriminate, and that's not never good.</description>
			<pubDate>Tue, 27 Apr 2004 18:08:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>Problems with Exceptions</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>I have a new problem with Exceptions: I need to manage the exception but I don't have an error code to compare with, just messages.  I can check a message to see if it has &quot;timeout&quot; then instruct my code to try again later, however, if the message is &quot;not found&quot; then I want to rethrow the exception or log it.  This can work fine, but the real problem comes when I install this on a system that produces exception strings in another language like Chinese, then I can no longer catch and manage using the message string.  Anyone, know how to handle this?</description>
			<pubDate>Wed, 28 Apr 2004 04:51:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>RE: Problems with exceptions</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>Different exceptions should have different types.  That way, you can check the exception type instead of checking the message.<br />
<br />
For your scenario, the throwing code should throw FileNotFoundExceptions and TimeoutExceptions (and any other distinct type which makes sense).  This permits code like:<br />
<br />
bool run = true;<br />
int attempts = 0;<br />
while (run) {<br />
____try {<br />
________FunctionThatThrows ();<br />
____}<br />
____catch (FileNotFoundException e) {<br />
________// don't bother retrying<br />
________run = false;<br />
____}<br />
____catch (TimeoutException e) {<br />
________// If it timed out, try again, but only re-try 5 times<br />
________if (++attempts &gt; 5) run = false;<br />
____}<br />
____catch (Exception e) {<br />
________// unknown exception; don't retry<br />
________run = false;<br />
____}<br />
}</description>
			<pubDate>Wed, 28 Apr 2004 13:14:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>&amp;quot;Use grammatically correct error messages, including ending punctuation&amp;quot;</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>Ah yes, we really should use &quot;WTF?????&quot; or &quot;WTF!!!! Why is this still happening?&quot;</description>
			<pubDate>Wed, 28 Apr 2004 13:36:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>re: omnivector</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>How would there be a null pointer excpetion?<br />
<br />
Your and your profs don't understand exception handling I guess.<br />
<br />
try{<br />
   File file = new File();<br />
    try{<br />
      // use the file here<br />
    }<br />
    catch(IOException e)<br />
    {<br />
       // log or rethrow, or whatever here<br />
    }<br />
    finally{<br />
      file.close();<br />
    }<br />
catch(FileCreateException fe)<br />
{<br />
  // handle whatever the File open exceptions are here<br />
}<br />
<br />
<br />
Where are you going to get a null pointer exception in this scheme?<br />
<br />
If an exception is thrown in the resource allocation (new File()), then execution will stop there and proceed to the catch FileCreateException.  This is how it's done by people who know what they are doing, I didn't invent it.</description>
			<pubDate>Wed, 28 Apr 2004 14:24:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>re: omnivector/phil</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>In defense of both of you, give 10 developers the same problem, you will get at least 11 different solutions.  Both ways work, it's just a matter of style; is one way any better than the other?  Well that would depends on your definition of better.  Going the ultra nested try/catch/finally route eliminates the need for null pointer checking.  Adding the null pointer checking eliminates the need for an nested level of try, et. al  People that know what they are doing do it both ways.  I've yet to see a solid argument for either side clearly showing one way it better than another.  If some one has one, I liked to take a read</description>
			<pubDate>Wed, 28 Apr 2004 19:42:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>

		<item>
			<title>re hamburgler</title>
			<link>http://osnews.com/thread?</link>
			<guid isPermaLink="true">http://osnews.com/thread?</guid>
			<description>Doing the null checks is more error prone.  and less clean.  that's it.  the other way does work though.<br />
<br />
He was saying my approach would throw null pointer exceptions though... and clearly it won't.  The reason people use the omnivector approach is 1) they don't realize that the variable assignment doesn't happen when an exception is thrown 2) they don't undertand that execution ends at the point of the exception and either leaves the method or goes straight to the catch/finally block.</description>
			<pubDate>Thu, 29 Apr 2004 00:42:00 GMT</pubDate>
			<author>donotreply@osnews.com (Anonymous)</author>
			<category>Comments</category>
		</item>
	</channel>
</rss>
