KeyNotFoundException, , . - , , , , . , , .
public static TValue GetOrThrow<TKey,TValue>(this IDictionary<TKey,TValue> d, TKey key)
{
try
{
return d[key];
}
catch(KeyNotFoundException ex)
{
throw new KeyNotFoundException(key.ToString()
+ " was not found in the dictionary");
}
}
/
key.ToString(), , ToString(). : "MyLibrary.SomeType was not found.", , "{ managerId: 123, employeeId: 456} was not found.".
json, :
var serializedKey = Newtonsoft.Json.JsonConvert.SerializeObject(
key,
new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
, , , , . ( , ). , :
throw new KeyNotFoundException(
"key was not found",
new KeyNotFoundException(serializedKey));