I have this code block that uses the ‘new’ dictionary (Xojo.Core.Dictionary) vs the ‘original’ Dictionary and the try catch block doesn’t trigger on exception:
App.Preferences is a Xojo.Core.Dictionary object
[code]’ if there is a preference value for the last viewed page panel, load it up and set the ‘page’ to that value
Try
If App.Preferences.Value(“mainWindowTab”) <> Nil Then ’ make sure there’s a a value
If IsNumeric (App.Preferences.Value(“mainWindowTab”)) Then ’ make sure it’s a number
Page.Value = App.Preferences.Value(“mainWindowTab”) ’ use the value
End If
End If
Catch err As KeyNotFoundException ’ log the error
' on first run and other situations where the mainWindowTab entry isn't present,
' an exception would be expected. just log it.
Easy.Log.Write (err.Message, "error", "", True, False)
End Try[/code]
KeyNotFoundException doesn’t seem to work for Xojo.Core.Dictionary.
I changed the first line to:
If App.Preferences.HasKey("mainWindowTab") = True
Which works.
I don’t understand why KeyNotFoundException isn’t ‘forwards compatible’ with Xojo.Core.Dictionary, but then again, I might be doing it wrong…
I’m talking about execution time. As I recall from when I tested a few years ago, raising one KeyNotFoundException is equal calling HasKey about 8 times. If you know the key might not be found, use one of the mechanisms provided (HasKey or Lookup) to compensate. Only if it would be unusual or rare for that key to be missing should you rely on the Exception.
BTW, there is nothing wrong with the way you did it, I’m just suggesting a more efficient alternative.
No worries. I’m curious because I’m a new user. How did you get that information about HasKey? Is there a tool XoJo has that allows you to see these details?
You mean the trade-off times? I was writing code where the Dictionary key sometimes could have been removed by another process so I wrote code to time which was faster. In that case, since it was a rare occurrence, I decided to catch the Exception since I expected that to happen less than every 8 checks. You can discover a lot of this just by writing your own tests.