Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. var dictionary = new Dictionary<string, string>();
  2. var val = dictionary["mykey"];
  3.  
  4. string val;
  5. if(dictionary.TryGetValue("mykey", out val))
  6. {
  7. //The key was found. The value is in val.
  8. }
  9. else
  10. {
  11. //The key was not present.
  12. }
  13.  
  14. private void Application_UnhandledException(object sender,
  15. ApplicationUnhandledExceptionEventArgs e)
  16. {
  17. var stackTrace = e.ExceptionObject.StackTrace;
  18. //Log the stackTrace somewhere.
  19. }
  20.  
  21. private void Application_UnhandledException(object sender,
  22. ApplicationUnhandledExceptionEventArgs e)
  23. {
  24. if (e.ExceptionObject is KeyNotFoundException)
  25. {
  26. //This was a KeyNotFoundException
  27. }
  28. }
  29.  
  30. public static TValue GetOrThrow<TKey,TValue>(this IDictionary<TKey,TValue> d, TKey key)
  31. {
  32. try
  33. {
  34. return d[key];
  35. }
  36. catch(KeyNotFoundException ex)
  37. {
  38. throw new KeyNotFoundException(key.ToString()
  39. + " was not found in the dictionary");
  40. }
  41. }
  42.  
  43. var serializedKey = Newtonsoft.Json.JsonConvert.SerializeObject(
  44. key,
  45. new JsonSerializerSettings
  46. {
  47. //make it easy for humans to read
  48. Formatting = Formatting.Indented,
  49. //don't break on loops...that would cause a new error that hides the KeyNotFound!
  50. ReferenceLoopHandling = ReferenceLoopHandling.Ignore
  51. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement