{"id":299,"date":"2012-10-18T10:11:12","date_gmt":"2012-10-18T10:11:12","guid":{"rendered":"http:\/\/nicholshayes.co.uk\/blog\/?p=299"},"modified":"2013-10-31T15:56:11","modified_gmt":"2013-10-31T15:56:11","slug":"playing-with-exceptions","status":"publish","type":"post","link":"http:\/\/nicholshayes.co.uk\/blog\/?p=299","title":{"rendered":"Playing with exceptions"},"content":{"rendered":"<p>I&#8217;ve just been having a little play with exceptions, as a new colleague wanted to understand how different errors could be handled. There is a good description of how they work in the &#8220;Exceptions, Catch, and Throw&#8221; chapter of the origin <a href=\"http:\/\/ruby-doc.org\/docs\/ProgrammingRuby\/\">Pickaxe book<\/a>.<\/p>\n<p>I created the following script to demonstrate how it works<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;height:300px;\"><div class=\"ruby codecolorer\"><span class=\"kw1\">module<\/span> Play<br \/>\n&nbsp; <span class=\"kw1\">def<\/span> <span class=\"kw2\">self<\/span>.<span class=\"kw1\">do<\/span> <span class=\"sy0\">&amp;<\/span>er<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">begin<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw3\">puts<\/span> <span class=\"st0\">&quot;<span class=\"es0\">\\n<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; er.<span class=\"me1\">call<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">rescue<\/span> <span class=\"kw4\">SyntaxError<\/span>, KeyError <span class=\"sy0\">=&gt;<\/span> e<br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw3\">puts<\/span> <span class=\"st0\">&quot;One of SyntaxError, KeyError&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; output<span class=\"br0\">&#40;<\/span>e<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">rescue<\/span> <span class=\"kw4\">RuntimeError<\/span> <span class=\"sy0\">=&gt;<\/span> e<br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw3\">puts<\/span> <span class=\"st0\">&quot;Another known error&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; output<span class=\"br0\">&#40;<\/span>e<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">rescue<\/span> <span class=\"sy0\">=&gt;<\/span> e<br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw3\">puts<\/span> <span class=\"st0\">'Unknown error'<\/span><br \/>\n&nbsp; &nbsp; &nbsp; output<span class=\"br0\">&#40;<\/span>e<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">end<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><br \/>\n<br \/>\n&nbsp; <span class=\"kw1\">def<\/span> <span class=\"kw2\">self<\/span>.<span class=\"me1\">output<\/span><span class=\"br0\">&#40;<\/span>error<span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">puts<\/span> <span class=\"st0\">&quot;#{error.class}: #{error.message}&quot;<\/span><br \/>\n&nbsp; <span class=\"kw1\">end<\/span><br \/>\n<span class=\"kw1\">end<\/span><br \/>\n<br \/>\nPlay.<span class=\"kw1\">do<\/span> <span class=\"br0\">&#123;<\/span><span class=\"kw3\">raise<\/span> <span class=\"kw4\">SyntaxError<\/span><span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\nPlay.<span class=\"kw1\">do<\/span> <span class=\"br0\">&#123;<\/span><span class=\"kw3\">raise<\/span> <span class=\"st0\">&quot;Gor Blimey&quot;<\/span><span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\nPlay.<span class=\"kw1\">do<\/span> <span class=\"br0\">&#123;<\/span>HeBeJeeBees<span class=\"br0\">&#125;<\/span><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve just been having a little play with exceptions, as a new colleague wanted to understand how different errors could be handled. There is a good description of how they work in the &#8220;Exceptions, Catch, and Throw&#8221; chapter of the &hellip; <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=299\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/299"}],"collection":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=299"}],"version-history":[{"count":9,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":464,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions\/464"}],"wp:attachment":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}