<?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>ph-lee &#187; begin</title>
	<atom:link href="http://www.ph-lee.com/tag/begin/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ph-lee.com</link>
	<description>aspire to inspire before you expire</description>
	<lastBuildDate>Sat, 29 May 2010 19:11:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to : Exceptions in ruby (on rails) coming from a Java or C# .NET background</title>
		<link>http://www.ph-lee.com/2009/07/14/how-to-exceptions-in-ruby-on-rails-coming-from-a-java-or-c-net-background/</link>
		<comments>http://www.ph-lee.com/2009/07/14/how-to-exceptions-in-ruby-on-rails-coming-from-a-java-or-c-net-background/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 13:25:54 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[begin]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[catch]]></category>
		<category><![CDATA[end]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[raise]]></category>
		<category><![CDATA[rescue]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[throw]]></category>
		<category><![CDATA[try]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=114</guid>
		<description><![CDATA[Personally coming from a Java and C# .NET background exceptions in ruby (on rails) seemed primative and rather lacking compared to the former languages I have had experiences in. To show off the differences and to show how to do exceptions in ruby lets start of with some examples.
C# .NET Example
Class (User)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// &#60;summary&#62;
/// constructor for [...]]]></description>
			<content:encoded><![CDATA[<p>Personally coming from a Java and C# .NET background exceptions in ruby (on rails) seemed primative and rather lacking compared to the former languages I have had experiences in. To show off the differences and to show how to do exceptions in ruby lets start of with some examples.</p>
<p>C# .NET Example<br />
Class (User)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// constructor for creating a new user</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;str_username&quot;&gt;username of the user&lt;/param&gt;</span>
&nbsp;
<span style="color: #0600FF;">public</span> User<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> str_username<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//check method parameters/arguments for null or empty</span>
&nbsp;
   <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>str_username<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//throw an exception since parameters entered are not valid</span>
      <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;argument str_username was null or empty&quot;</span>, <span style="color: #666666;">&quot;str_username&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">str_username</span> <span style="color: #008000;">=</span> str_username<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// absurd method for the purpose of this example</span>
<span style="color: #008080; font-style: italic;">/// creates two users</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> createBobAndEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//try and create bob</span>
&nbsp;
   <span style="color: #0600FF;">try</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008000;">new</span> User<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Bob&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ArgumentException e<span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//try and create empty</span>
&nbsp;
   <span style="color: #0600FF;">try</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008000;">new</span> User<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ArgumentException e<span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//this exception will be thrown in this case</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">//e is the variable storing the exception which we can print the exception message, variable of concern</span>
      <span style="color: #008080; font-style: italic;">//in other cases the entire stack trace can be printed for debugging purposes</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">//in this particular case it would be best to log the error and simply notify the user they can't create a user with empty string as username</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">finally</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//this finally block is optional and normally used for garbage collection/closing connections/closing files etc</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Java Example<br />
Class (User)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * constructor for creating a new User object
 *
 * @param str_username the username of the user
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> User<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> str_username<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">//check parameter is not null</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>str_username <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NullPointerException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;str_username is null&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//check parameter is not an empty string</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>str_username.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;str_username is empty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">str_username</span> <span style="color: #339933;">=</span> str_username<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * absurd method for the purpose of this example
 * creates 2 users
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> createBobAndEmpty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">//try and create the user bob</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">try</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bob&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NullPointerException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalArgumentException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//try and create user empty</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">try</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NullPointerException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalArgumentException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will be thrown in this case</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//e is the variable storing the exception which we can print the exception message, variable of concern</span>
      <span style="color: #666666; font-style: italic;">//in other cases the entire stack trace can be printed for debugging purposes</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//in this particular case it would be best to log the error and simply notify the user they can't create a user with empty string as username</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">finally</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this finally block is optional and normally used for garbage collection/closing connections/closing files etc</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ruby</p>
<p>Firstly Ruby uses different terms/keywords compared to Java or C#, the usual try/catch/throw/finally are different. Instead&#8230;</p>
<ul>
<li>&#8220;try&#8221; becomes &#8220;begin&#8221;</li>
<li>&#8220;catch&#8221; becomes &#8220;rescue&#8221;</li>
<li>&#8220;finally&#8221; becomes &#8220;ensure&#8221;</li>
</ul>
<p>So in Ruby<br />
Class (User)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#class constructor for a user</span>
<span style="color:#008000; font-style:italic;">#username represents the username for the new user</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>username<span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#008000; font-style:italic;">#check argument username is not an empty string</span>
   <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#9966CC; font-weight:bold;">if</span> username.<span style="color:#9900CC;">empty</span>? <span style="color:#008000; font-style:italic;">#keyword &quot;raised&quot; used instead of &quot;throw&quot;</span>
&nbsp;
   <span style="color:#0066ff; font-weight:bold;">@username</span> = username
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#absurd method for the purpose of this article</span>
<span style="color:#008000; font-style:italic;">#create 2 users</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> User.<span style="color:#9900CC;">create_bob_and_empty</span>
   <span style="color:#008000; font-style:italic;">#try and create user user Bob</span>
&nbsp;
   <span style="color:#9966CC; font-weight:bold;">begin</span> <span style="color:#008000; font-style:italic;">#this keyword is used instead of &quot;try&quot;</span>
      User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Bob&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      <span style="color:#008000; font-style:italic;">#this rescue clause will not be performed in this example</span>
   <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#008000; font-style:italic;">#this clause will be performed since none of the above rescue clauses were fired</span>
      <span style="color:#008000; font-style:italic;">#can be used to report success of operations where no exceptions were encountered</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
   <span style="color:#008000; font-style:italic;">#try and create user empty</span>
&nbsp;
   <span style="color:#9966CC; font-weight:bold;">begin</span>
      User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Bob&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      <span style="color:#008000; font-style:italic;">#this clause will be fired in this case</span>
&nbsp;
      <span style="color:#008000; font-style:italic;">#the variable e holds the exception that has been raised and can be used to trace exceptions stack</span>
   <span style="color:#9966CC; font-weight:bold;">ensure</span>
      <span style="color:#008000; font-style:italic;">#this clause will be fired in this case</span>
      <span style="color:#008000; font-style:italic;">#this is equivalent to the finally cluse in java or csharp</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>So that completes the examples, now you can compare the differences between ruby and the other languages. The <a href="http://blog.nicksieger.com/articles/2006/09/06/rubys-exception-hierarchy">ruby exception hierarchy [Sieger]</a> in comparisonm to Java and C# is rather small and limited and it is likely that you will need to <a href="http://rpheath.com/posts/237-raising-custom-exceptions-in-rails">create your own exceptions [Heath]</a> by extending the hierarchy in your application. Before reading Heath&#8217;s article I should warn you his examples are poor as he uses exception for normal control-flow of logic rather than using exception for error handling which what it should only be used for.</p>
<p>Another thing you may also find useful is the keyword &#8220;retry&#8221;. This word/statement can be put in any rescue clause to restart the begin/end block which in lies in. This maybe useful to re-attempt to parse a file or connect to databse etc.</p>
<p>In ruby on top of begin/raise/rescue/end, throw/catch can also be used. Here&#8217;s an example&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">catch</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:done</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
   a = <span style="color:#006666;">1</span>
   <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#0000FF; font-weight:bold;">true</span>
      throw <span style="color:#ff3333; font-weight:bold;">:done</span> <span style="color:#9966CC; font-weight:bold;">if</span> a == <span style="color:#006666;">100</span>
      a<span style="color:#006600; font-weight:bold;">++</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>In ruby, throw does not throw an exception instead it throws a string or :symbol. So as far I can tell it is only used for control-flow purposes rather than the classical case of exception handling, unless someone can tell me otherwise.</p>
<p>Hope you found this article useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/07/14/how-to-exceptions-in-ruby-on-rails-coming-from-a-java-or-c-net-background/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
