KeyNotFoundException information

I have a C # Silverlight application that accidentally throws a " KeyNotFoundException". I have no idea which key cannot be found. This led me to two questions:

  • Does the KeyNotFoundExceptionkey that he was trying to find retain? When I looked through the documentation , I did not see anything that implied that this information was available.
  • I catch / log this exception in the general Application.UnhandledException event handler. My question is: if I catch the event here, can I throw an ExceptionObject into a KeyNotFoundException and still get the key information if it is set as specified in # 1?

Thank you so much for your help!

+5
source share
4 answers

A is KeyNotFoundExceptioncaused by an attempt to get a value from the Dictionary with a given key when the key is missing. For instance:

var dictionary = new Dictionary<string, string>();
var val = dictionary["mykey"];

You can look at all the places where the dictionary is used and identify yourself. A common best practice for this is that you are looking for a meaning in a dictionary that might be missing - to use it TryGetValue. Catching an exception each time is a more expensive operation and is not needed:

string val;
if(dictionary.TryGetValue("mykey", out val))
{
     //The key was found. The value is in val.
}
else
{
    //The key was not present.
}

You can look at a property StackTracefor them KeyNotFoundExceptionto pinpoint where this problem is. All exceptions have a property StackTrace, so you don't have to worry about what type of exception was in your global error handler. For instance:

private void Application_UnhandledException(object sender, 
        ApplicationUnhandledExceptionEventArgs e)
    {
         var stackTrace = e.ExceptionObject.StackTrace;
         //Log the stackTrace somewhere.
    }

Or, if you want to know what type of exception:

private void Application_UnhandledException(object sender, 
        ApplicationUnhandledExceptionEventArgs e)
    {
         if (e.ExceptionObject is KeyNotFoundException)
         {
             //This was a KeyNotFoundException
         }
    }
+4
source

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
    {
        //make it easy for humans to read
        Formatting = Formatting.Indented, 
        //don't break on loops...that would cause a new error that hides the KeyNotFound!
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore 
    });

, , , , . ( , ). , :
throw new KeyNotFoundException( "key was not found", new KeyNotFoundException(serializedKey));

+4

, - StackTrace Application.UnhandledException. StackTrace ( , , ). , , . , .

+3

This is because silverlight for Windows Phone does not support wsHttpBinding, so you change the service to basicHttpBinding and it will work

0
source

All Articles