Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit db342eb

Browse files
Anipikdanmoseley
authored andcommitted
added keu value to key not found exception (#15201)
1 parent bbc97f6 commit db342eb

File tree

9 files changed

+15
-12
lines changed

9 files changed

+15
-12
lines changed

src/mscorlib/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@
426426
</data>
427427
<data name="Arg_KeyNotFound" xml:space="preserve">
428428
<value>The given key was not present in the dictionary.</value>
429+
</data>
430+
<data name="Arg_KeyNotFoundWithKey" xml:space="preserve">
431+
<value>The given key '{0}' was not present in the dictionary.</value>
429432
</data>
430433
<data name="Arg_LongerThanDestArray" xml:space="preserve">
431434
<value>Destination array was not long enough. Check the destination index, length, and the array's lower bounds.</value>

src/mscorlib/shared/System/Collections/Generic/Dictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public TValue this[TKey key]
212212
{
213213
int i = FindEntry(key);
214214
if (i >= 0) return entries[i].value;
215-
ThrowHelper.ThrowKeyNotFoundException();
215+
ThrowHelper.ThrowKeyNotFoundException(key);
216216
return default(TValue);
217217
}
218218
set

src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/EventPayload.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public object this[string key]
5353
position++;
5454
}
5555

56-
throw new System.Collections.Generic.KeyNotFoundException();
56+
throw new System.Collections.Generic.KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
5757
}
5858
set
5959
{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ public TValue this[TKey key]
652652
TValue value;
653653
if (!TryGetValue(key, out value))
654654
{
655-
ThrowKeyNotFoundException();
655+
ThrowKeyNotFoundException(key);
656656
}
657657
return value;
658658
}
@@ -669,9 +669,9 @@ public TValue this[TKey key]
669669
// of important methods like TryGetValue and ContainsKey.
670670

671671
[MethodImpl(MethodImplOptions.NoInlining)]
672-
private static void ThrowKeyNotFoundException()
672+
private static void ThrowKeyNotFoundException(object key)
673673
{
674-
throw new KeyNotFoundException();
674+
throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
675675
}
676676

677677
[MethodImpl(MethodImplOptions.NoInlining)]

src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/ConstantSplittableMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public TValue Lookup(TKey key)
100100

101101
if (!found)
102102
{
103-
Exception e = new KeyNotFoundException(SR.Arg_KeyNotFound);
103+
Exception e = new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
104104
e.SetErrorCode(HResults.E_BOUNDS);
105105
throw e;
106106
}

src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/DictionaryToMapAdapter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal V Lookup<K, V>(K key)
4040

4141
if (!keyFound)
4242
{
43-
Exception e = new KeyNotFoundException(SR.Arg_KeyNotFound);
43+
Exception e = new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
4444
e.SetErrorCode(HResults.E_BOUNDS);
4545
throw e;
4646
}
@@ -95,7 +95,7 @@ internal void Remove<K, V>(K key)
9595

9696
if (!removed)
9797
{
98-
Exception e = new KeyNotFoundException(SR.Arg_KeyNotFound);
98+
Exception e = new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
9999
e.SetErrorCode(HResults.E_BOUNDS);
100100
throw e;
101101
}

src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/IMapViewToIReadOnlyDictionaryAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private static V Lookup<K, V>(IMapView<K, V> _this, K key)
111111
catch (Exception ex)
112112
{
113113
if (HResults.E_BOUNDS == ex._HResult)
114-
throw new KeyNotFoundException(SR.Arg_KeyNotFound);
114+
throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
115115
throw;
116116
}
117117
}

src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/IReadOnlyDictionaryToIMapViewAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal V Lookup<K, V>(K key)
3939

4040
if (!keyFound)
4141
{
42-
Exception e = new KeyNotFoundException(SR.Arg_KeyNotFound);
42+
Exception e = new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
4343
e.SetErrorCode(HResults.E_BOUNDS);
4444
throw e;
4545
}

src/mscorlib/src/System/ThrowHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ internal static void ThrowAddingDuplicateWithKeyArgumentException(object key)
120120
throw GetAddingDuplicateWithKeyArgumentException(key);
121121
}
122122

123-
internal static void ThrowKeyNotFoundException()
123+
internal static void ThrowKeyNotFoundException(object key)
124124
{
125-
throw new KeyNotFoundException();
125+
throw new KeyNotFoundException(key.ToString());
126126
}
127127

128128
internal static void ThrowArgumentException(ExceptionResource resource)

0 commit comments

Comments
 (0)