<?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>Jayway Team Blog &#187; exception</title>
	<atom:link href="http://blog.jayway.com/tag/exception/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Sat, 11 Feb 2012 10:33:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Ruby, an Exceptional Language</title>
		<link>http://blog.jayway.com/2011/05/25/ruby-an-exceptional-language/</link>
		<comments>http://blog.jayway.com/2011/05/25/ruby-an-exceptional-language/#comments</comments>
		<pubDate>Wed, 25 May 2011 09:47:59 +0000</pubDate>
		<dc:creator>Anders Janmyr</dc:creator>
				<category><![CDATA[Dynamic languages]]></category>
		<category><![CDATA[continuations]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=8536</guid>
		<description><![CDATA[Based on the book Exceptional Ruby by Avdi Grimm, I have developed a strategy for how I should deal with exceptions in Ruby. Being a very dynamic language, Ruby allows very flexible coding techniques. Exceptions are not an exception . When I am developing a library in Ruby I typically create one Error module and [...]]]></description>
			<content:encoded><![CDATA[<p>Based on the book <a href="http://exceptionalruby.com/">Exceptional Ruby</a> by Avdi Grimm, I have developed a strategy for how I should deal with exceptions in Ruby. </p>
<p>Being a very dynamic language, Ruby allows very flexible coding techniques.  Exceptions are not an exception <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>When I am developing a library in Ruby I typically create one <code>Error</code> module and one <code>StdError</code> class. The <code>Error</code> module is a typical <em>tag module</em> and does not contain any methods.</p>
<h2>Tag Module</h2>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900"># Tag module for the Tapir library</font></i>
<b><font color="#0000FF">module</font></b> Tapir
  <b><font color="#0000FF">module</font></b> Error
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b> 

</tt></pre>
<p>The reason for the tag module is that I can use it to tag exceptions<br />
occurring inside my library without having to wrap them in a nested<br />
exception.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">module</font></b> Tapir
  <b><font color="#0000FF">class</font></b> Downloader

    <b><font color="#0000FF">def</font></b> <b><font color="#0000FF">self</font></b><font color="#990000">.</font>get url
      HTTP<font color="#990000">.</font>get url
    <b><font color="#0000FF">rescue</font></b> StandardError <font color="#990000">-&gt;</font> error   <i><font color="#9A1900"># Rescue the error</font></i>
      <i><font color="#9A1900"># Namespace the error by tagging it with ::Tapir::Error</font></i>
      error<font color="#990000">.</font>extend<font color="#990000">(::</font>Tapir<font color="#990000">::</font>Error<font color="#990000">)</font>
      <b><font color="#0000FF">raise</font></b>                         <i><font color="#9A1900"># And raise it again</font></i>
    <b><font color="#0000FF">end</font></b> 

  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># Client usage</font></i>
<b><font color="#0000FF">begin</font></b>
  Tapir<font color="#990000">::</font>Downloader<font color="#990000">.</font>get <font color="#FF0000">'http://non.existent.url/'</font>
<b><font color="#0000FF">rescue</font></b> Tapir<font color="#990000">::</font>Error <font color="#990000">=&gt;</font> error
  puts <font color="#FF0000">"Stupid tapir, gave me error #{error.message}"</font>
<b><font color="#0000FF">end</font></b> 

</tt></pre>
<p>This is beautiful. I am scoping an internal error as my own. Since Ruby<br />
is dynamic there is no need to declare a new class that wraps all the<br />
methods in the <code>StandarError</code> I have access to them anyway. Duck typing<br />
for the win!</p>
<h2>A Nested Exception Class</h2>
<p>In some cases the tag module is not enough. Perhaps the exception was<br />
not created by another exception. In that case I need a real class since<br />
it is not possible to raise modules. But while I am at it I usually make<br />
the class a nested exception in order to simplify wrapping of other<br />
exceptions if the need comes up. This is how I do that.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">module</font></b> Tapir
  <i><font color="#9A1900"># I usually call the class `StdError` since it prevents the user of</font></i>
  <i><font color="#9A1900"># the library from rescuing the global `StandardError`.</font></i>
  <b><font color="#0000FF">class</font></b> StdError <font color="#990000">&lt;</font> StandardError
    extend Error             <i><font color="#9A1900"># Extend the Error tag module</font></i>
    attr_reader <font color="#990000">:</font>original    <i><font color="#9A1900"># Add an accessor for the original, if one exists</font></i> 

    <i><font color="#9A1900"># Create the error with a message and an original that defaults to</font></i>
    <i><font color="#9A1900"># the exception that is currently active, in this thread, if one exists</font></i>
    <b><font color="#0000FF">def</font></b> initialize<font color="#990000">(</font>msg<font color="#990000">,</font> original<font color="#990000">=</font>$<font color="#990000">!)</font>
      <b><font color="#0000FF">super</font></b><font color="#990000">(</font>msg<font color="#990000">)</font>
      <font color="#009900">@original</font> <font color="#990000">=</font> original<font color="#990000">;</font>
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># Client Usage</font></i>
<b><font color="#0000FF">begin</font></b>
  Tapir<font color="#990000">.</font>do_something_that_fails
<b><font color="#0000FF">rescue</font></b> Tapir<font color="#990000">::</font>Error <font color="#990000">=&gt;</font> error      <i><font color="#9A1900"># rescue the tag module</font></i>
  puts <font color="#FF0000">"Bad tapir #{error.message}, due to #{error.original.message}"</font>
<b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># or if I want to be more specific</font></i>
<b><font color="#0000FF">begin</font></b>
  Tapir<font color="#990000">.</font>do_something_that_fails
<b><font color="#0000FF">rescue</font></b> Tapir<font color="#990000">::</font>StdError <font color="#990000">=&gt;</font> error   <i><font color="#9A1900"># rescue the specific error</font></i>
  puts <font color="#FF0000">"Bad tapir #{error.message}, due to #{error.original.message}"</font>
<b><font color="#0000FF">end</font></b> 

</tt></pre>
<p>Notice that I don&rsquo;t have to wrap the exception explicitly, since<br />
I default the Exception to the last error that is stored in <code>$!</code>. </p>
<p>Now the only reason for me to want to create a <code>Tapir::StdError</code> apart<br />
from it being misuse of my library is if I want to add additional information<br />
to the exception that already occurred. In that case I may also want to<br />
extend the <code>Tapir::StdError</code> and create an exception with additional<br />
fields.</p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><b><font color="#0000FF">module</font></b> Tapir

  <i><font color="#9A1900"># Create a specific exception to add more information for the client</font></i>
  <b><font color="#0000FF">class</font></b> TooOldError <font color="#990000">&lt;</font> StdError
    attr_reader <font color="#990000">:</font>age<font color="#990000">,</font> <font color="#990000">:</font>max_age

    <b><font color="#0000FF">def</font></b> initialize<font color="#990000">(</font>msg<font color="#990000">,</font> original<font color="#990000">=</font>$<font color="#990000">!,</font> age<font color="#990000">,</font> max_age<font color="#990000">)</font>
      <b><font color="#0000FF">super</font></b><font color="#990000">(</font>msg<font color="#990000">,</font> original<font color="#990000">)</font>
      <font color="#009900">@age</font><font color="#990000">,</font> <font color="#009900">@max_age</font> <font color="#990000">=</font> age<font color="#990000">,</font> max_age
    <b><font color="#0000FF">end</font></b>
  <b><font color="#0000FF">end</font></b>
<b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># Client usage</font></i>
<b><font color="#0000FF">begin</font></b>
  tapir<font color="#990000">.</font>mate<font color="#990000">(</font>other_tapir<font color="#990000">)</font>
<b><font color="#0000FF">rescue</font></b> TooOldError <font color="#990000">=&gt;</font> error
  <i><font color="#9A1900"># Use the specific error properties</font></i>
  puts <font color="#FF0000">"Hey, your are #{error.age}, that is too damn old!"</font>
<b><font color="#0000FF">end</font></b> 

</tt></pre>
<h2>Throw &ndash; Catch</h2>
<p>Ruby also has an alternative to <code>raise</code> and <code>rescue</code> called <code>throw</code> and<br />
<code>catch</code>.</p>
<p>They should not be used as an alternative to exceptions, instead they are<br />
<a href="http://en.wikipedia.org/wiki/Continuation#Kinds_of_continuations">escape continuations</a><br />
that should be used to escape from nested control structures across<br />
method calls. Powerful! Here is an example from <a href="http://www.sinatrarb.com/">Sinatra</a></p>
<p><!-- Generator: GNU source-highlight 3.1.4<br />
by Lorenzo Bettini</p>
<p>http://www.lorenzobettini.it</p>
<p>http://www.gnu.org/software/src-highlite --> </p>
<pre><tt><i><font color="#9A1900"># Here is the throw</font></i> 

   <i><font color="#9A1900"># Pass control to the next matching route.</font></i>
    <i><font color="#9A1900"># If there are no more matching routes, Sinatra will</font></i>
    <i><font color="#9A1900"># return a 404 response.</font></i>
    <b><font color="#0000FF">def</font></b> pass<font color="#990000">(&amp;</font>block<font color="#990000">)</font>
      <b><font color="#0000FF">throw</font></b> <font color="#990000">:</font>pass<font color="#990000">,</font> block
    <b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># and here is where it is caught</font></i> 

    <b><font color="#0000FF">def</font></b> process_route<font color="#990000">(</font>pattern<font color="#990000">,</font> keys<font color="#990000">,</font> conditions<font color="#990000">)</font>
      <font color="#990000">...</font>
      <b><font color="#0000FF">catch</font></b><font color="#990000">(:</font>pass<font color="#990000">)</font> <b><font color="#0000FF">do</font></b>
        conditions<font color="#990000">.</font>each <font color="#FF0000">{</font> <font color="#990000">|</font>cond<font color="#990000">|</font>
          <b><font color="#0000FF">throw</font></b> <font color="#990000">:</font>pass <b><font color="#0000FF">if</font></b> instance_eval<font color="#990000">(&amp;</font>cond<font color="#990000">)</font> <font color="#990000">==</font> <b><font color="#0000FF">false</font></b> <font color="#FF0000">}</font>
        <b><font color="#0000FF">yield</font></b>
      <b><font color="#0000FF">end</font></b>
    <b><font color="#0000FF">end</font></b> 

<i><font color="#9A1900"># Allowing usage such as</font></i> 

  get <font color="#FF0000">'/guess/:who'</font> <b><font color="#0000FF">do</font></b>
    pass <b><font color="#0000FF">unless</font></b> params<font color="#990000">[:</font>who<font color="#990000">]</font> <font color="#990000">==</font> <font color="#FF0000">'Frank'</font>
    <font color="#FF0000">'You got me!'</font>
  <b><font color="#0000FF">end</font></b> 

  get <font color="#FF0000">'/guess/*'</font> <b><font color="#0000FF">do</font></b>
    <font color="#FF0000">'You missed!'</font>
  <b><font color="#0000FF">end</font></b> 

</tt></pre>
<p>Lovely!</p>
<h2>Wrap up</h2>
<p>This is how I use exceptions in Ruby now, thanks to ideas from the book.<br />
Other good ideas from the book are the three guarantees:</p>
<ul>
<li><em>The weak guarantee</em>, if an exception is raised, the object will be in<br />
a consistent state.</li>
<li><em>The strong guarantee</em>, if an exception is raised, the object will be<br />
left in its initial state.</li>
<li><em>The nothrow guarantee</em>, no exceptions will be raised by this method.</li>
</ul>
<p>And a nice way of categorizing exceptions based on three different<br />
usages by the client. (My categories are not exactly the same as Avdis)</p>
<ul>
<li><em>User Error</em>, the client has used the library wrong.</li>
<li><em>Internal Error</em>, something is wrong with the library. We are looking<br />
into the problem&hellip;</li>
<li><em>Transient Error</em>, something is now working right now, but the same<br />
call may succeed in a while. It is a good idea to provide a period<br />
after whick the call will probably succeed.<br />
the client to try again.</li>
</ul>
<p>It is a great book which contains a lot more information than I covered<br />
here. Get it, it is well worth the money.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2011/05/25/ruby-an-exceptional-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sneaky throw</title>
		<link>http://blog.jayway.com/2010/01/29/sneaky-throw/</link>
		<comments>http://blog.jayway.com/2010/01/29/sneaky-throw/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 15:10:03 +0000</pubDate>
		<dc:creator>Jan Kronquist</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[jayview]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=4517</guid>
		<description><![CDATA[The latest issue of our magazine Jayview is out with brand new look and feel. My contribution was a cool piece of code that gets rid of those annoying exceptions. However, during layouting the link to the source disappeared. Credit should of course go to Reinier Zwitserloot and his mail on Java Posse. public class [...]]]></description>
			<content:encoded><![CDATA[<p>The latest issue of <a href="http://jayway.com/jayculture/jayview">our magazine Jayview</a> is out with brand new look and feel. My contribution was a cool piece of code that gets rid of those annoying exceptions. However, during layouting the link to the source disappeared. Credit should of course go to <a href="http://zwitserloot.com/">Reinier Zwitserloot</a> and <a href="http://www.mail-archive.com/javaposse@googlegroups.com/msg05984.html">his mail on Java Posse</a>. </p>
<pre class="brush:java">
public class Sneak {
    public static RuntimeException sneakyThrow(Throwable t) {
        if ( t == null ) throw new NullPointerException("t");
        Sneak.&lt;RuntimeException&gt;sneakyThrow0(t);
        return null;
    }

    @SuppressWarnings("unchecked")
    private static &lt;T extends Throwable&gt; void sneakyThrow0(Throwable t) throws T {
        throw (T)t;
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/01/29/sneaky-throw/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

