Merge branch 'master' of https://github.com/mono/mono into issue4328
[mono.git] / mcs / class / System / System.Collections.Generic / SortedList.cs
index 2a4ccc4fc6f4a34697fd514845583442a68bfb14..c1190b2ad26d67daeeecc6c79191814e56db3879 100644 (file)
@@ -464,12 +464,7 @@ namespace System.Collections.Generic
                        if (key == null)
                                throw new ArgumentNullException ("key");
 
-                       int indx = 0;
-                       try {
-                               indx = Find (key);
-                       } catch (Exception) {
-                               throw new InvalidOperationException();
-                       }
+                       int indx = Find (key);
 
                        return (indx | (indx >> 31));
                }
@@ -559,13 +554,7 @@ namespace System.Collections.Generic
 
                        KeyValuePair<TKey, TValue> [] table = this.table;
 
-                       int freeIndx = -1;
-
-                       try {
-                               freeIndx = Find (key);
-                       } catch (Exception) {
-                               throw new InvalidOperationException();
-                       }
+                       int freeIndx = Find (key);
 
                        if (freeIndx >= 0) {
                                if (!overwrite)
@@ -620,6 +609,15 @@ namespace System.Collections.Generic
                        }
                }
 
+               private int Compare (TKey a, TKey b)
+               {
+                       try {
+                               return comparer.Compare (a, b);
+                       } catch (Exception ex) {
+                               throw new InvalidOperationException ("Failed to compare two elements.", ex);
+                       }
+               }
+
                private int Find (TKey key)
                {
                        KeyValuePair<TKey, TValue> [] table = this.table;
@@ -633,7 +631,7 @@ namespace System.Collections.Generic
                        while (left <= right) {
                                int guess = (left + right) >> 1;
 
-                               int cmp = comparer.Compare (table[guess].Key, key);
+                               int cmp = Compare (table[guess].Key, key);
                                if (cmp == 0) return guess;
 
                                if (cmp <  0) left = guess+1;