[SOLVED] Looking for examples on handling errors and validations in Qt?



  • I get that people working in Qt prefer error codes. But then how do I manage the error codes? Having habituated to using exception for well, exception handling, I can't wrap my head around using error codes.

    I wish there were a good, neat example that demonstrates errror/exception handling in Qt. I would appreciate links to such examples.


  • Lifetime Qt Champion

    Hi,

    Generally when handling error codes there are two possibilities:

    Stop the flow of execution if there are any errors

    React on specific error(s)

    For number one, an if condition is what is used
    For number two, you can use a switch statement or an if, else if block

    Also depending on your work flow, you might propagate the error further

    Hope it helps



  • Ok. That is useful.

    And how do I manage all the error codes? What I have thought of is to use enum for each class, that would contain error codes throwable by that class.

    However, I am still confused as to where to put human-readable error messages. I feel that the same class is not the place, as it could be desirable to display different error messages for the same error code.
    My current approach is to have ApplicationController class implement a method that maps the error codes to readable messages.

    Any ideas on that?

    [quote author="SGaist" date="1376256485"]Hi,

    Generally when handling error codes there are two possibilities:

    Stop the flow of execution if there are any errors

    React on specific error(s)

    For number one, an if condition is what is used
    For number two, you can use a switch statement or an if, else if block

    Also depending on your work flow, you might propagate the error further

    Hope it helps[/quote]


  • Lifetime Qt Champion

    You can have an error class that you can return and query for additional informations, like QSqlError or QDBusError



  • An error class makes the most sense, indeed. I will select that option. Thanks!

    [quote author="SGaist" date="1376338394"]You can have an error class that you can return and query for additional informations, like QSqlError or QDBusError
    [/quote]


  • Lifetime Qt Champion

    You're welcome !

    One other thing, if one of your error class is used to show a string version of your errors in a GUI, you might want to translate them



  • Yes, I guess I have to use Qt Resource system for that?

    [quote author="SGaist" date="1376394515"]You're welcome !

    One other thing, if one of your error class is used to show a string version of your errors in a GUI, you might want to translate them[/quote]


  • Lifetime Qt Champion

    Why are your thinking about the resources system ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.