Skip to content

Commit fb12870

Browse files
benaadamsjkotas
authored andcommitted
ConcurrentDictionary<TKey, TValue> Exceptions to ThrowHelper (dotnet/coreclr#7079)
Commit migrated from dotnet/coreclr@b8b530f
1 parent 31a60a1 commit fb12870

File tree

2 files changed

+64
-56
lines changed

2 files changed

+64
-56
lines changed

src/coreclr/src/mscorlib/src/System/Collections/Concurrent/ConcurrentDictionary.cs

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public ConcurrentDictionary(IEqualityComparer<TKey> comparer) : this(DefaultConc
229229
public ConcurrentDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
230230
: this(comparer)
231231
{
232-
if (collection == null) throw new ArgumentNullException("collection");
232+
if (collection == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
233233

234234
InitializeFromCollection(collection);
235235
}
@@ -259,8 +259,8 @@ public ConcurrentDictionary(
259259
int concurrencyLevel, IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
260260
: this(concurrencyLevel, DEFAULT_CAPACITY, false, comparer)
261261
{
262-
if (collection == null) throw new ArgumentNullException("collection");
263-
if (comparer == null) throw new ArgumentNullException("comparer");
262+
if (collection == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
263+
if (comparer == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer);
264264

265265
InitializeFromCollection(collection);
266266
}
@@ -270,11 +270,11 @@ private void InitializeFromCollection(IEnumerable<KeyValuePair<TKey, TValue>> co
270270
TValue dummy;
271271
foreach (KeyValuePair<TKey, TValue> pair in collection)
272272
{
273-
if (pair.Key == null) throw new ArgumentNullException("key");
273+
if (pair.Key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
274274

275275
if (!TryAddInternal(pair.Key, pair.Value, false, false, out dummy))
276276
{
277-
throw new ArgumentException(GetResource("ConcurrentDictionary_SourceContainsDuplicateKeys"));
277+
ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_SourceContainsDuplicateKeys);
278278
}
279279
}
280280

@@ -312,13 +312,13 @@ internal ConcurrentDictionary(int concurrencyLevel, int capacity, bool growLockA
312312
{
313313
if (concurrencyLevel < 1)
314314
{
315-
throw new ArgumentOutOfRangeException("concurrencyLevel", GetResource("ConcurrentDictionary_ConcurrencyLevelMustBePositive"));
315+
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.concurrencyLevel, ExceptionResource.ConcurrentDictionary_ConcurrencyLevelMustBePositive);
316316
}
317317
if (capacity < 0)
318318
{
319-
throw new ArgumentOutOfRangeException("capacity", GetResource("ConcurrentDictionary_CapacityMustNotBeNegative"));
319+
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ConcurrentDictionary_CapacityMustNotBeNegative);
320320
}
321-
if (comparer == null) throw new ArgumentNullException("comparer");
321+
if (comparer == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer);
322322

323323
// The capacity should be at least as large as the concurrency level. Otherwise, we would have locks that don't guard
324324
// any buckets.
@@ -358,7 +358,7 @@ internal ConcurrentDictionary(int concurrencyLevel, int capacity, bool growLockA
358358
/// contains too many elements.</exception>
359359
public bool TryAdd(TKey key, TValue value)
360360
{
361-
if (key == null) throw new ArgumentNullException("key");
361+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
362362
TValue dummy;
363363
return TryAddInternal(key, value, false, true, out dummy);
364364
}
@@ -375,7 +375,7 @@ public bool TryAdd(TKey key, TValue value)
375375
/// (Nothing in Visual Basic).</exception>
376376
public bool ContainsKey(TKey key)
377377
{
378-
if (key == null) throw new ArgumentNullException("key");
378+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
379379

380380
TValue throwAwayValue;
381381
return TryGetValue(key, out throwAwayValue);
@@ -395,7 +395,7 @@ public bool ContainsKey(TKey key)
395395
/// (Nothing in Visual Basic).</exception>
396396
public bool TryRemove(TKey key, out TValue value)
397397
{
398-
if (key == null) throw new ArgumentNullException("key");
398+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
399399

400400
return TryRemoveInternal(key, out value, false, default(TValue));
401401
}
@@ -486,7 +486,7 @@ private bool TryRemoveInternal(TKey key, out TValue value, bool matchValue, TVal
486486
[SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread safety")]
487487
public bool TryGetValue(TKey key, out TValue value)
488488
{
489-
if (key == null) throw new ArgumentNullException("key");
489+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
490490

491491
int bucketNo, lockNoUnused;
492492

@@ -531,7 +531,7 @@ public bool TryGetValue(TKey key, out TValue value)
531531
[SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread safety")]
532532
public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue)
533533
{
534-
if (key == null) throw new ArgumentNullException("key");
534+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
535535

536536
IEqualityComparer<TValue> valueComparer = EqualityComparer<TValue>.Default;
537537

@@ -642,8 +642,8 @@ public void Clear()
642642
[SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "ConcurrencyCop just doesn't know about these locks")]
643643
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
644644
{
645-
if (array == null) throw new ArgumentNullException("array");
646-
if (index < 0) throw new ArgumentOutOfRangeException("index", GetResource("ConcurrentDictionary_IndexIsNegative"));
645+
if (array == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
646+
if (index < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ConcurrentDictionary_IndexIsNegative);
647647

648648
int locksAcquired = 0;
649649
try
@@ -659,7 +659,7 @@ void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[]
659659

660660
if (array.Length - count < index || count < 0) //"count" itself or "count + index" can overflow
661661
{
662-
throw new ArgumentException(GetResource("ConcurrentDictionary_ArrayNotLargeEnough"));
662+
ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_ArrayNotLargeEnough);
663663
}
664664

665665
CopyToPairs(array, index);
@@ -956,13 +956,13 @@ public TValue this[TKey key]
956956
TValue value;
957957
if (!TryGetValue(key, out value))
958958
{
959-
throw new KeyNotFoundException();
959+
ThrowHelper.ThrowKeyNotFoundException();
960960
}
961961
return value;
962962
}
963963
set
964964
{
965-
if (key == null) throw new ArgumentNullException("key");
965+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
966966
TValue dummy;
967967
TryAddInternal(key, value, true, true, out dummy);
968968
}
@@ -1026,8 +1026,8 @@ public int Count
10261026
/// if the key was not in the dictionary.</returns>
10271027
public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
10281028
{
1029-
if (key == null) throw new ArgumentNullException("key");
1030-
if (valueFactory == null) throw new ArgumentNullException("valueFactory");
1029+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
1030+
if (valueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.valueFactory);
10311031

10321032
TValue resultingValue;
10331033
if (TryGetValue(key, out resultingValue))
@@ -1052,7 +1052,7 @@ public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
10521052
/// key is already in the dictionary, or the new value if the key was not in the dictionary.</returns>
10531053
public TValue GetOrAdd(TKey key, TValue value)
10541054
{
1055-
if (key == null) throw new ArgumentNullException("key");
1055+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
10561056

10571057
TValue resultingValue;
10581058
TryAddInternal(key, value, false, true, out resultingValue);
@@ -1080,9 +1080,9 @@ public TValue GetOrAdd(TKey key, TValue value)
10801080
/// absent) or the result of updateValueFactory (if the key was present).</returns>
10811081
public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
10821082
{
1083-
if (key == null) throw new ArgumentNullException("key");
1084-
if (addValueFactory == null) throw new ArgumentNullException("addValueFactory");
1085-
if (updateValueFactory == null) throw new ArgumentNullException("updateValueFactory");
1083+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
1084+
if (addValueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.addValueFactory);
1085+
if (updateValueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.updateValueFactory);
10861086

10871087
TValue newValue, resultingValue;
10881088
while (true)
@@ -1127,8 +1127,8 @@ public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKe
11271127
/// absent) or the result of updateValueFactory (if the key was present).</returns>
11281128
public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
11291129
{
1130-
if (key == null) throw new ArgumentNullException("key");
1131-
if (updateValueFactory == null) throw new ArgumentNullException("updateValueFactory");
1130+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
1131+
if (updateValueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.updateValueFactory);
11321132
TValue newValue, resultingValue;
11331133
while (true)
11341134
{
@@ -1207,7 +1207,7 @@ void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
12071207
{
12081208
if (!TryAdd(key, value))
12091209
{
1210-
throw new ArgumentException(GetResource("ConcurrentDictionary_KeyAlreadyExisted"));
1210+
ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_KeyAlreadyExisted);
12111211
}
12121212
}
12131213

@@ -1340,8 +1340,7 @@ bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
13401340
/// name="keyValuePair"/> is a null reference (Nothing in Visual Basic).</exception>
13411341
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> keyValuePair)
13421342
{
1343-
if (keyValuePair.Key == null) throw new ArgumentNullException(GetResource("ConcurrentDictionary_ItemKeyIsNull"));
1344-
1343+
if (keyValuePair.Key == null) ThrowHelper.ThrowArgumentNullException(ExceptionResource.ConcurrentDictionary_ItemKeyIsNull);
13451344
TValue throwAwayValue;
13461345
return TryRemoveInternal(keyValuePair.Key, out throwAwayValue, true, keyValuePair.Value);
13471346
}
@@ -1387,17 +1386,17 @@ IEnumerator IEnumerable.GetEnumerator()
13871386
/// </exception>
13881387
void IDictionary.Add(object key, object value)
13891388
{
1390-
if (key == null) throw new ArgumentNullException("key");
1391-
if (!(key is TKey)) throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfKeyIncorrect"));
1389+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
1390+
if (!(key is TKey)) ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfKeyIncorrect);
13921391

1393-
TValue typedValue;
1392+
TValue typedValue = default(TValue);
13941393
try
13951394
{
13961395
typedValue = (TValue)value;
13971396
}
13981397
catch (InvalidCastException)
13991398
{
1400-
throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfValueIncorrect"));
1399+
ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfValueIncorrect);
14011400
}
14021401

14031402
((IDictionary<TKey, TValue>)this).Add((TKey)key, typedValue);
@@ -1415,7 +1414,7 @@ void IDictionary.Add(object key, object value)
14151414
/// (Nothing in Visual Basic).</exception>
14161415
bool IDictionary.Contains(object key)
14171416
{
1418-
if (key == null) throw new ArgumentNullException("key");
1417+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
14191418

14201419
return (key is TKey) && ((ConcurrentDictionary<TKey, TValue>)this).ContainsKey((TKey)key);
14211420
}
@@ -1475,7 +1474,7 @@ ICollection IDictionary.Keys
14751474
/// (Nothing in Visual Basic).</exception>
14761475
void IDictionary.Remove(object key)
14771476
{
1478-
if (key == null) throw new ArgumentNullException("key");
1477+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
14791478

14801479
TValue throwAwayValue;
14811480
if (key is TKey)
@@ -1517,7 +1516,7 @@ object IDictionary.this[object key]
15171516
{
15181517
get
15191518
{
1520-
if (key == null) throw new ArgumentNullException("key");
1519+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
15211520

15221521
TValue value;
15231522
if (key is TKey && this.TryGetValue((TKey)key, out value))
@@ -1529,10 +1528,10 @@ object IDictionary.this[object key]
15291528
}
15301529
set
15311530
{
1532-
if (key == null) throw new ArgumentNullException("key");
1531+
if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
15331532

1534-
if (!(key is TKey)) throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfKeyIncorrect"));
1535-
if (!(value is TValue)) throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfValueIncorrect"));
1533+
if (!(key is TKey)) ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfKeyIncorrect);
1534+
if (!(value is TValue)) ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfValueIncorrect);
15361535

15371536
((ConcurrentDictionary<TKey, TValue>)this)[(TKey)key] = (TValue)value;
15381537
}
@@ -1563,8 +1562,8 @@ object IDictionary.this[object key]
15631562
[SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "ConcurrencyCop just doesn't know about these locks")]
15641563
void ICollection.CopyTo(Array array, int index)
15651564
{
1566-
if (array == null) throw new ArgumentNullException("array");
1567-
if (index < 0) throw new ArgumentOutOfRangeException("index", GetResource("ConcurrentDictionary_IndexIsNegative"));
1565+
if (array == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
1566+
if (index < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ConcurrentDictionary_IndexIsNegative);
15681567

15691568
int locksAcquired = 0;
15701569
try
@@ -1581,7 +1580,7 @@ void ICollection.CopyTo(Array array, int index)
15811580

15821581
if (array.Length - count < index || count < 0) //"count" itself or "count + index" can overflow
15831582
{
1584-
throw new ArgumentException(GetResource("ConcurrentDictionary_ArrayNotLargeEnough"));
1583+
ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_ArrayNotLargeEnough);
15851584
}
15861585

15871586
// To be consistent with the behavior of ICollection.CopyTo() in Dictionary<TKey,TValue>,
@@ -1611,7 +1610,7 @@ void ICollection.CopyTo(Array array, int index)
16111610
return;
16121611
}
16131612

1614-
throw new ArgumentException(GetResource("ConcurrentDictionary_ArrayIncorrectType"), "array");
1613+
ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_ArrayIncorrectType, ExceptionArgument.array);
16151614
}
16161615
finally
16171616
{
@@ -1641,7 +1640,8 @@ object ICollection.SyncRoot
16411640
{
16421641
get
16431642
{
1644-
throw new NotSupportedException(Environment.GetResourceString("ConcurrentCollection_SyncRoot_NotSupported"));
1643+
ThrowHelper.ThrowNotSupportedException(ExceptionResource.ConcurrentCollection_SyncRoot_NotSupported);
1644+
return default(object);
16451645
}
16461646
}
16471647

@@ -1976,18 +1976,6 @@ private void Assert(bool condition)
19761976
Contract.Assert(condition);
19771977
}
19781978

1979-
/// <summary>
1980-
/// A helper function to obtain the string for a particular resource key.
1981-
/// </summary>
1982-
/// <param name="key"></param>
1983-
/// <returns></returns>
1984-
private string GetResource(string key)
1985-
{
1986-
Assert(key != null);
1987-
1988-
return Environment.GetResourceString(key);
1989-
}
1990-
19911979
/// <summary>
19921980
/// A node in a singly-linked list representing a particular hash table bucket.
19931981
/// </summary>

src/coreclr/src/mscorlib/src/System/ThrowHelper.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ internal static void ThrowArgumentNullException(ExceptionArgument argument) {
8484
throw new ArgumentNullException(GetArgumentName(argument));
8585
}
8686

87+
internal static void ThrowArgumentNullException(ExceptionResource resource)
88+
{
89+
throw new ArgumentNullException(Environment.GetResourceString(GetResourceName(resource)));
90+
}
91+
8792
internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument) {
8893
throw new ArgumentOutOfRangeException(GetArgumentName(argument));
8994
}
@@ -247,6 +252,10 @@ internal enum ExceptionArgument {
247252
beginMethod,
248253
continuationOptions,
249254
continuationAction,
255+
valueFactory,
256+
addValueFactory,
257+
updateValueFactory,
258+
concurrencyLevel,
250259

251260
}
252261

@@ -341,6 +350,17 @@ internal enum ExceptionResource {
341350
TaskCompletionSourceT_TrySetException_NullException,
342351
TaskCompletionSourceT_TrySetException_NoExceptions,
343352
InvalidOperation_WrongAsyncResultOrEndCalledMultiple,
353+
ConcurrentDictionary_ConcurrencyLevelMustBePositive,
354+
ConcurrentDictionary_CapacityMustNotBeNegative,
355+
ConcurrentDictionary_TypeOfValueIncorrect,
356+
ConcurrentDictionary_TypeOfKeyIncorrect,
357+
ConcurrentDictionary_SourceContainsDuplicateKeys,
358+
ConcurrentDictionary_KeyAlreadyExisted,
359+
ConcurrentDictionary_ItemKeyIsNull,
360+
ConcurrentDictionary_IndexIsNegative,
361+
ConcurrentDictionary_ArrayNotLargeEnough,
362+
ConcurrentDictionary_ArrayIncorrectType,
363+
ConcurrentCollection_SyncRoot_NotSupported,
344364

345365
}
346366
}

0 commit comments

Comments
 (0)