How to keep list of error code and messages in the program

  1. last week
    Edited last week

    Sorry, the title may be not so straight forward.

    I want to keep a list of error code and messages with in the program, and not in database or external file.

    My initial approach was to create a struct with error code and error message and save it as a constant with dictionary data type.

    Unfortunately constant can only be set to primal data type such as integer, string, boolean & text.

    The only way that I think can be done is to create a computed property in a class where I will have to set all the structure values by code.

    Would there be a elegant way to implement this?

    I've seen it done where to stay in the concept of Exceptions for Errors, the developer had created several subclasses of RuntimeException that set their Message property in the constructor.

    I don't tend to do it that way, but I do use lots of RuntimeException subclasses in my error handling designs.

  2. Kevin G

    May 23 Xojo Pro Gatesheed, England

    Maybe constants or a dictionary.

  3. @Kevin G Maybe constants or a dictionary.

    I just said that the constant does not work.
    How are you implying to use a constant?

    A constant for all the error messages?

  4. Beatrix W

    May 23 Testers, Third Party Store Europe (Germany)

    What do you want to do with your list?

    Make a class with the properties you need and then make a manager for the properties class. You don't need to use constants.

  5. Julian S

    May 23 Xojo Pro UK

    The answer to this question will change the implementation options.

    Do you want the list of errors to show in the IDE using autocomplete or just use them at runtime?

  6. @Julian S The answer to this question will change the implementation options.

    Do you want the list of errors to show in the IDE using autocomplete or just use them at runtime?

    The former.

  7. @Beatrix W What do you want to do with your list?

    Make a class with the properties you need and then make a manager for the properties class. You don't need to use constants.

    Meaning I have to create a property for all the error codes. Correct?

  8. Julian S

    May 23 Xojo Pro UK

    Have you tried creating a global Module and having the error codes as Constants with the default string value as the message?

  9. @Julian S Have you tried creating a global Module and having the error codes as Constants with the default string value as the message?

    I suppose that is the best solution so far. I wanted to use the struct, but no luck.

  10. Beatrix W

    May 24 Testers, Third Party Store Europe (Germany)

    Show us your code.

  11. Markus R

    May 24 Testers, Xojo Pro Europe / Germany / Lower Saxon...
    Edited last week

    i would use a module with constant as example
    name errorfilenotfound
    string = "4711:file not found"
    you have number and info in one and the method that show this error could split the number and value if necessary.
    and you have the option for localization.

    and for other error you can store/output the exception object.

    docs.xojo.com/Raise
    docs.xojo.com/RuntimeException

  12. Tim P

    May 24 Testers, Xojo Pro Answer Rochester, NY

    I've seen it done where to stay in the concept of Exceptions for Errors, the developer had created several subclasses of RuntimeException that set their Message property in the constructor.

    I don't tend to do it that way, but I do use lots of RuntimeException subclasses in my error handling designs.

  13. @Tim P I've seen it done where to stay in the concept of Exceptions for Errors, the developer had created several subclasses of RuntimeException that set their Message property in the constructor.

    I don't tend to do it that way, but I do use lots of RuntimeException subclasses in my error handling designs.

    This would be the correct way, but managing the all the exceptions in Xojo IDE is not so easy to maintain.

  14. Anthony C

    May 24 Testers, Xojo Pro, XDC Speakers, Third Party Store, Forum Moderators, MVP GraffitiSuite Developer
    Edited last week

    @Dong KHam This would be the correct way, but managing the all the exceptions in Xojo IDE is not so easy to maintain.

    You could create a module to manage your specific exceptions with functions for creating new ones. This would give you the autocomplete functionality you likely desire. For instance, I might have a module called ProjectExceptions with a method called BadColorException that I can use if a user selected two colors that are too close together in cartesian distance, or might be hard for a color blind person to make out. This would, in turn, instantiate a new instance of my RuntimeException subclass called BadColorException.

    I've created this example for you to look at.

  15. Anthony C

    May 24 Testers, Xojo Pro, XDC Speakers, Third Party Store, Forum Moderators, MVP GraffitiSuite Developer
    Edited last week

    Note that my example is just one method, and not a great one, for doing this. You could just as easily do like @Tim P said and override the constructors of the RuntimeException subclasses that you add to the module for the same effect with the following code:

    var newException as new ProjectExceptions.BadColorException
  16. Tim P

    May 25 Testers, Xojo Pro Rochester, NY

    @Anthony C Note that my example is just one method, and not a great one, for doing this. You could just as easily do like @Tim P said and override the constructors of the RuntimeException subclasses that you add to the module for the same effect with the following code:
    var newException as new ProjectExceptions.BadColorException

    In the message in constructor design, the developer was doing this:

    raise new ProjectExceptions.BadColorException

    Which is really clever, but doesn't offer specifics so it's a cost-benefit judgement.

or Sign Up to reply!