Posts Tagged ‘end’

How to : Exceptions in ruby (on rails) coming from a Java or C# .NET background

Posted in Uncategorized on July 14th, 2009 by ph-lee – Be the first to comment

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
/// <summary>
/// constructor for creating a new user
/// </summary>
/// <param name="str_username">username of the user</param>
 
public User(string str_username)
{
   //check method parameters/arguments for null or empty
 
   if (String.IsNullOrEmpty(str_username))
   {
      //throw an exception since parameters entered are not valid
      throw new ArgumentException("argument str_username was null or empty", "str_username");
   }
 
   this.str_username = str_username;
}
 
/// <summary>
/// absurd method for the purpose of this example
/// creates two users
/// </summary>
 
public static void createBobAndEmpty()
{
   //try and create bob
 
   try
   {
      new User("Bob");
   }
   catch (ArgumentException e)
   {
      //this exception will not be thrown in this case
   }
 
   //try and create empty
 
   try
   {
      new User("");
   }
   catch (ArgumentException e)
   {
      //this exception will be thrown in this case
 
      //e is the variable storing the exception which we can print the exception message, variable of concern
      //in other cases the entire stack trace can be printed for debugging purposes
 
      //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
   }
   finally
   {
      //this finally block is optional and normally used for garbage collection/closing connections/closing files etc
   }
}

Java 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * constructor for creating a new User object
 *
 * @param str_username the username of the user
 */
 
public User(String str_username)
{
   //check parameter is not null
 
   if(str_username == null)
   {
      throw new NullPointerException("str_username is null");
   }
 
   //check parameter is not an empty string
 
   if(str_username.isEmpty())
   {
      throw new IllegalArgumentException("str_username is empty");
   }
 
   this.str_username = str_username;
}
 
/**
 * absurd method for the purpose of this example
 * creates 2 users
 */
 
public static void createBobAndEmpty()
{
   //try and create the user bob
 
   try
   {
      new User("Bob");
   }
   catch (NullPointerException e)
   {
      //this exception will not be thrown in this case
   }
   catch (IllegalArgumentException e)
   {
      //this exception will not be thrown in this case
   }
 
   //try and create user empty
 
   try
   {
      new User("");
   }
   catch (NullPointerException e)
   {
      //this exception will not be thrown in this case
   }
   catch (IllegalArgumentException e)
   {
      //this exception will be thrown in this case
 
      //e is the variable storing the exception which we can print the exception message, variable of concern
      //in other cases the entire stack trace can be printed for debugging purposes
 
      //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
   }
   finally
   {
      //this finally block is optional and normally used for garbage collection/closing connections/closing files etc
   }
}

Ruby

Firstly Ruby uses different terms/keywords compared to Java or C#, the usual try/catch/throw/finally are different. Instead…

  • “try” becomes “begin”
  • “catch” becomes “rescue”
  • “finally” becomes “ensure”

So in Ruby
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
#class constructor for a user
#username represents the username for the new user
 
def initialize(username)
   #check argument username is not an empty string
   raise ArgumentError if username.empty? #keyword "raised" used instead of "throw"
 
   @username = username
end
 
#absurd method for the purpose of this article
#create 2 users
 
def User.create_bob_and_empty
   #try and create user user Bob
 
   begin #this keyword is used instead of "try"
      User.new("Bob")
   rescue ArgumentError => e
      #this rescue clause will not be performed in this example
   else
      #this clause will be performed since none of the above rescue clauses were fired
      #can be used to report success of operations where no exceptions were encountered
   end
 
   #try and create user empty
 
   begin
      User.new("Bob")
   rescue ArgumentError => e
      #this clause will be fired in this case
 
      #the variable e holds the exception that has been raised and can be used to trace exceptions stack
   ensure
      #this clause will be fired in this case
      #this is equivalent to the finally cluse in java or csharp
   end
end

So that completes the examples, now you can compare the differences between ruby and the other languages. The ruby exception hierarchy [Sieger] in comparisonm to Java and C# is rather small and limited and it is likely that you will need to create your own exceptions [Heath] by extending the hierarchy in your application. Before reading Heath’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.

Another thing you may also find useful is the keyword “retry”. 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.

In ruby on top of begin/raise/rescue/end, throw/catch can also be used. Here’s an example…

1
2
3
4
5
6
7
catch(:done) do
   a = 1
   while true
      throw :done if a == 100
      a++
   end
end

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.

Hope you found this article useful.