Try-Catch & Dictionary

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…

Works as expected here in Windows using Xojo 2018r4.

https://www.dropbox.com/s/xybtl29gim3bq0c/TestForRob.xojo_binary_project?dl=1

Would you be able to share a simple project with it happening incorrectly?

Sure. I’d be happy to :slight_smile:

This code block is part of my larger project, but I can make something based on this that I can share. I’ll post on the weekend.

Suggestion: store the value in a variable through Lookup, then test the variable, something like this:

dim mainWindowTab as string = App.Preferences.Lookup("mainWindowTab", "")
if mainWindowTab <> "" then
...

FYI, Exceptions are “expensive” and you should rely on them rarely.

@Kem Tekinay what do you mean by expensive? Is there some documentation I can look at?

I read these:
http://documentation.xojo.com/api/exceptions/exception.html
http://documentation.xojo.com/api/language/runtime.htmlException

And they don’t caution about expense (assuming in terms of memory or cpu) - they seem to encourage the use of these constructs.

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.

Or did I misunderstand your question?

No, that was it. I was wondering how you learned about the expense. Thanks for sharing that.

More info: https://documentation.xojo.com/getting_started/debugging/exception_handling.html

@ - I figured it out :slight_smile:

I don’t know the IDE that well yet, but the “break on exceptions” setting caused the code to stop here:

If App.Preferences.Value("mainWindowTab") <> Nil Then

Which made me think the exception handler was not working when it was. When I turned that off, and ran the code, it worked perfectly.

https://www.dropbox.com/s/zmtlqxidermr6ke/Dictionary.xojo_binary_project?dl=1

Thanks everyone for helping - I learned something valuable today lol :slight_smile:

[quote=424268:@Rob Hallock]@ - I figured it out :slight_smile:

I don’t know the IDE that well yet, but the “break on exceptions” setting caused the code to stop here:

If App.Preferences.Value("mainWindowTab") <> Nil Then

Which made me think the exception handler was not working when it was. When I turned that off, and ran the code, it worked perfectly.

https://www.dropbox.com/s/zmtlqxidermr6ke/Dictionary.xojo_binary_project?dl=1

Thanks everyone for helping - I learned something valuable today lol :)[/quote]
That’s a great place to use Lookup instead of Value

If App.Preferences.Lookup("mainWindowTab", Nil) <> Nil Then

FYI - turning off Break On Exceptions does not change whether an exception occurs there, only whether the debugger stops.