Posts Tagged ‘exception’

C# .NET Exception stack hierachy

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

After getting to know ruby’s exceptions I decided to revisit them in C# .NET. Ruby has a small set of exceptions which developers can use in comparison to .NET framework which has many more. Although I do like the MSDN Library, I couldn’t find a quick and easy way of seeing an overview of all the exceptions built in the framework for browsing. After being inspired by [Sieger] in his Ruby’s Exception Hierarchy article I’d thought I do the same for C# .NET, and here’s the result…

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
System.Exception
   System.SystemException
      System.OutOfMemoryException
         System.InsufficientMemoryException
      System.StackOverflowException
      System.DataMisalignedException
      System.ExecutionEngineException
      System.MemberAccessException
         System.FieldAccessException
         System.MethodAccessException
         System.MissingMemberException
            System.MissingFieldException
            System.MissingMethodException
      System.AccessViolationException
      System.AppDomainUnloadedException
      System.ArgumentException
         System.ArgumentNullException
         System.ArgumentOutOfRangeException
         System.DuplicateWaitObjectException
         System.Text.DecoderFallbackException
         System.Text.EncoderFallbackException
      System.ArithmeticException
         System.DivideByZeroException
         System.NotFiniteNumberException
         System.OverflowException
      System.ArrayTypeMismatchException
      System.BadImageFormatException
      System.CannotUnloadAppDomainException
      System.TypeUnloadedException
      System.ContextMarshalException
      System.TypeLoadException
         System.EntryPointNotFoundException
         System.DllNotFoundException
      System.FormatException
         System.Reflection.CustomAttributeFormatException
      System.IndexOutOfRangeException
      System.InvalidCastException
      System.InvalidOperationException
         System.ObjectDisposedException
      System.InvalidProgramException
      System.MulticastNotSupportedException
      System.NotImplementedException
      System.NotSupportedException
         System.PlatformNotSupportedException
      System.NullReferenceException
      System.OperationCanceledException
      System.RankException
      System.TimeoutException
      System.TypeInitializationException
      System.UnauthorizedAccessException
         System.Security.AccessControl.PrivilegeNotHeldException
      System.Threading.AbandonedMutexException
      System.Threading.SynchronizationLockException
      System.Threading.ThreadAbortException
      System.Threading.ThreadInterruptedException
      System.Threading.ThreadStateException
      System.Threading.ThreadStartException
      System.Collections.Generic.KeyNotFoundException
      System.Reflection.AmbiguousMatchException
      System.Reflection.ReflectionTypeLoadException
      System.Runtime.Serialization.SerializationException
      System.Resources.MissingManifestResourceException
      System.Resources.MissingSatelliteAssemblyException
      System.Security.Policy.PolicyException
      System.Runtime.InteropServices.ExternalException
         System.Runtime.InteropServices.COMException
         System.Runtime.InteropServices.SEHException
      System.Runtime.InteropServices.InvalidOleVariantTypeException
      System.Runtime.InteropServices.MarshalDirectiveException
      System.Runtime.InteropServices.InvalidComObjectException
      System.Runtime.InteropServices.SafeArrayRankMismatchException
      System.Runtime.InteropServices.SafeArrayTypeMismatchException
      System.IO.IOException
         System.IO.DirectoryNotFoundException
         System.IO.DriveNotFoundException
         System.IO.EndOfStreamException
         System.IO.FileLoadException
         System.IO.FileNotFoundException
         System.IO.PathTooLongException
      System.Security.XmlSyntaxException
      System.Security.SecurityException
      System.Security.HostProtectionException
      System.Security.VerificationException
      System.Runtime.Remoting.RemotingException
         System.Runtime.Remoting.RemotingTimeoutException
      System.Runtime.Remoting.ServerException
      System.Security.Cryptography.CryptographicException
         System.Security.Cryptography.CryptographicUnexpectedOperationException
      System.Security.Principal.IdentityNotMappedException
   System.ApplicationException
      System.Threading.WaitHandleCannotBeOpenedException
      System.Reflection.InvalidFilterCriteriaException
      System.Reflection.TargetException
      System.Reflection.TargetInvocationException
      System.Reflection.TargetParameterCountException
   System.ASSERT
   System.Reflection.MetadataException
   System.Runtime.CompilerServices.RuntimeWrappedException
   System.IO.IsolatedStorage.IsolatedStorageException

By seeing the hierachy in full it should help developers manage their exceptions, expecially when trying to manage deep call stacks and ensuring they are handled in the correct order, helping to pin-point errors more precisely.

I’ve posted the source code used to generate the above output over at github.

Hope this has helped.

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.