Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Looking for examples on handling errors and validations in Qt?
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Ghost
    wrote on last edited by
    #1

    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.

    Bhoot

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Ghost
        wrote on last edited by
        #3

        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]

        Bhoot

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Ghost
            wrote on last edited by
            #5

            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]

            Bhoot

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • G Offline
                G Offline
                Ghost
                wrote on last edited by
                #7

                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]

                Bhoot

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Why are your thinking about the resources system ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved