<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flirting With Models &#187; exceptions</title>
	<atom:link href="http://coreyhoffstein.com/tag/exceptions/feed/" rel="self" type="application/rss+xml" />
	<link>http://coreyhoffstein.com</link>
	<description>musings on numerical computing and financial modeling</description>
	<lastBuildDate>Mon, 30 Aug 2010 21:53:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Aren&#8217;t exceptions supposed to make things safer?</title>
		<link>http://coreyhoffstein.com/2009/01/15/arent-exceptions-supposed-to-make-things-safer/</link>
		<comments>http://coreyhoffstein.com/2009/01/15/arent-exceptions-supposed-to-make-things-safer/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 02:50:45 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coreyhoffstein.com/?p=38</guid>
		<description><![CDATA[Weren&#8217;t things easier in the days of error codes?  Then along came exceptions and took errors to a whole new level.  Unfortunately, they also changed the rules of flow control, and somehow made writing &#8216;safe&#8217; code more difficult. Cleaning up after an error, especially a fatal error, is very important.  Exceptions, ironically, can make this [...]]]></description>
			<content:encoded><![CDATA[<p>Weren&#8217;t things easier in the days of error codes?  Then along came exceptions and took errors to a whole new level.  Unfortunately, they also changed the rules of flow control, and somehow made writing &#8216;safe&#8217; code more difficult.</p>
<p>Cleaning up after an error, especially a fatal error, is very important.  Exceptions, ironically, can make this an even more difficult task, as <a href="http://www.ddj.com/cpp/184403758" target="_blank">this</a> article astutely points out.  For those of you too lazy to make the jump, basically imagine a situation where you call several functions in a row that may throw exceptions, each of which requires a unique clean-up.  To handle such a situation, you would require nested try/catch statements, which is just ugly.</p>
<p>Or, better, we can use call-backs to restore state to pre-exception status.  In the article, this was achieved by using a ScopeGuard object, which held a function pointer that would be called on destruction &#8212; but only in the case where the object wasn&#8217;t &#8216;dismissed&#8217; first.  If the object is dismissed, it need not be called.</p>
<p>Being a rubyist, I immediately tried to port this concept over to Ruby.  At face, it doesn&#8217;t seem quite as useful at first: the &#8216;Ruby&#8217; way typically has dangerous objects monitor and clean up after themselves using yield statements.  For example, when we open a file, we typically pass a block to be executed with the open file &#8212; but only in the case that the file is successfully opened.</p>
<p>On the other hand, imagine the case where we are trying to synchronize writes between two resources.  Perhaps we are using memory as a cache, but also storing to disk.  Let us also assume that failure to write to either resource ends up in an exception, forcing the other resource to roll-back to its previous state.  Handling the nesting of exceptions could get ugly.  Enter <a href="http://coreyhoffstein.com/wp-content/uploads/2009/01/scope_guard.rb">scope_guard</a>.</p>
<p>Instead of creating a class like the above article does, I made my scope guard a function that takes an object, function name, and parameters.  If a block is given, the function yields, wrapping the statement in a begin/rescue block.  In the case of an exception, our &#8216;undo&#8217; function is performed on the object.  Ruby makes it all simple and easy to do.</p>
<p>Imagine usage:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> write<span style="color:#006600; font-weight:bold;">&#40;</span>cache, disk, data<span style="color:#006600; font-weight:bold;">&#41;</span>
   cache.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#41;</span>
   scope_guard<span style="color:#006600; font-weight:bold;">&#40;</span>cache, <span style="color:#ff3333; font-weight:bold;">:roll_back</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
      disk.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now you can see that if the disk fails to write the data, the scope-guard ensures that the cache is rolled-back.  In the other case, where the cache fails to write, the exception raises before the data is written to disk, so state is maintained.</p>
<p>The system isn&#8217;t without flaws, however.  It falls short in one of the simplest of cases: an integer counter.  If I am trying to maintain a count of objects I write to disk, I have to use a wrapper around the basic integer object, because operations on Fixnum return a new object instead of altering the object in question.  Not the end of the world, but not very clean either.  I am currently trying to wrap my head around possible elegant solutions for this.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://coreyhoffstein.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://coreyhoffstein.com/2009/01/15/arent-exceptions-supposed-to-make-things-safer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
