Try-Catch & Dictionary

  1. 10 months ago

    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

    ' 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

    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?

  2. Julian S

    8 Feb 2019 Pre-Release Testers, Xojo Pro Answer UK

    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?

  3. Sure. I'd be happy to :)

    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.

  4. Kem T

    8 Feb 2019 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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.

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

    I read these:
    http://docs.xojo.com/Exception
    http://docs.xojo.com/RuntimeException

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

  6. Kem T

    8 Feb 2019 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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.

  7. 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?

  8. Kem T

    8 Feb 2019 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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?

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

  10. Paul L

    8 Feb 2019 Xojo Inc, Third Party Store

    More info: https://docs.xojo.com/UserGuide:Exception_Handling

  11. @Julian S - I figured it out :)

    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 :)

  12. Greg O

    10 Feb 2019 Xojo Inc scout.galaxy.barn
    Edited 10 months ago

    @Rob H @Julian S - I figured it out :)

    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 :)

    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.

or Sign Up to reply!