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. Exceptions and Qt
Qt 6.11 is out! See what's new in the release blog

Exceptions and Qt

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 6 Posters 2.4k Views 2 Watching
  • 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by
    #1

    In my code there are a number of places where exceptions are thrown that are intended to be terminal if thrown.

    Qt has recently started complaining about these saying :

    Qt has caught an exception thrown from an event handler. Throwing
    exceptions from an event handler is not supported in Qt.
    You must not let any exception whatsoever propagate through Qt code.
    

    is that really so true if I expect that exception to terminate the application with prejudice.

    David

    JoeCFDJ S 2 Replies Last reply
    0
    • aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #12

      @Perdrix There has been a thread about Exceptions on the mailing list half a year ago: https://lists.qt-project.org/pipermail/development/2024-May/045252.html

      You might get more insights about the problematics of Exceptions especially on different architectures from there.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      0
      • PerdrixP Perdrix

        In my code there are a number of places where exceptions are thrown that are intended to be terminal if thrown.

        Qt has recently started complaining about these saying :

        Qt has caught an exception thrown from an event handler. Throwing
        exceptions from an event handler is not supported in Qt.
        You must not let any exception whatsoever propagate through Qt code.
        

        is that really so true if I expect that exception to terminate the application with prejudice.

        David

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #2

        @Perdrix If exceptions are caught and handled in the middle of your app, your app does not bail out.

        1 Reply Last reply
        0
        • PerdrixP Offline
          PerdrixP Offline
          Perdrix
          wrote on last edited by Perdrix
          #3

          They are not caught in my code except at the end of main() to report them and terminate.

          There are places where exceptions are thrown and caught locally when they are recoverable, but there are some situations that I deliberately allow to result in them terminating the application

          JoeCFDJ S 2 Replies Last reply
          0
          • PerdrixP Perdrix

            They are not caught in my code except at the end of main() to report them and terminate.

            There are places where exceptions are thrown and caught locally when they are recoverable, but there are some situations that I deliberately allow to result in them terminating the application

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #4

            @Perdrix Qt has caught an exception, but did not exit.

            1 Reply Last reply
            0
            • PerdrixP Offline
              PerdrixP Offline
              Perdrix
              wrote on last edited by
              #5

              Qt rethrew the exception AFAICT as it was reported by the catch code at the end of main() before termination
              D.

              Christian EhrlicherC 1 Reply Last reply
              0
              • PerdrixP Perdrix

                Qt rethrew the exception AFAICT as it was reported by the catch code at the end of main() before termination
                D.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Perdrix said in Exceptions and Qt:

                Qt rethrew the exception AFAICT

                No, not even in Qt4:

                https://github.com/qt/qt/blob/0a2f2382541424726168804be2c90b91381608c6/src/corelib/kernel/qeventloop.cpp#L198

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SamiV123
                  wrote on last edited by
                  #7

                  Well, the situation is kinda clear. Officially Qt does not allow you to propagate exceptions from a slot or event handler or anything that Qt calls.

                  I expect that the code has been written as if there are no exceptions that means that stack unwinding that happens due to exception can a) leak resources b) cause unexpected state and behavior. Might work (or appear to work) once in a while just to cause silent corruption and issues later on.

                  1 Reply Last reply
                  0
                  • PerdrixP Perdrix

                    They are not caught in my code except at the end of main() to report them and terminate.

                    There are places where exceptions are thrown and caught locally when they are recoverable, but there are some situations that I deliberately allow to result in them terminating the application

                    S Offline
                    S Offline
                    SamiV123
                    wrote on last edited by
                    #8

                    @Perdrix said in Exceptions and Qt:

                    There are places where exceptions are thrown and caught locally when they are recoverable

                    Sounds like an incorrect use of exceptions tbh. You should not use exceptions for logical conditions that are part of the expected program execution and code flow.

                    1 Reply Last reply
                    0
                    • PerdrixP Offline
                      PerdrixP Offline
                      Perdrix
                      wrote on last edited by Perdrix
                      #9

                      Its not there as part of expected flow its there to catch a totally invalid condition.

                      And the exception if caught by Qt MUST have been rethrown - how else could it end up being caught by the catch clause at the end of main.

                      In fact the code to which you pointed me DOES VERY CLEARLY rethrow the exception:

                      catch (...) {
                              qWarning("Qt has caught an exception thrown from an event handler. Throwing\n"
                                       "exceptions from an event handler is not supported in Qt. You must\n"
                                       "reimplement QApplication::notify() and catch all exceptions there.\n");
                      
                              // copied from below
                              locker.relock();
                              QEventLoop *eventLoop = d->threadData->eventLoops.pop();
                              Q_ASSERT_X(eventLoop == this, "QEventLoop::exec()", "internal error");
                              Q_UNUSED(eventLoop); // --release warning
                              d->inExec = false;
                              --d->threadData->loopLevel;
                      
                              throw;
                          }
                      

                      THIS IS INTENDED TO TERMINATE THE APPLICATION WITH PREJUDICE

                      David

                      S 1 Reply Last reply
                      0
                      • PerdrixP Perdrix

                        In my code there are a number of places where exceptions are thrown that are intended to be terminal if thrown.

                        Qt has recently started complaining about these saying :

                        Qt has caught an exception thrown from an event handler. Throwing
                        exceptions from an event handler is not supported in Qt.
                        You must not let any exception whatsoever propagate through Qt code.
                        

                        is that really so true if I expect that exception to terminate the application with prejudice.

                        David

                        S Offline
                        S Offline
                        SimonSchroeder
                        wrote on last edited by
                        #10

                        @Perdrix said in Exceptions and Qt:

                        Qt has recently started complaining about these

                        I don't think this is new. As far as I know this has always been there.

                        If you want to terminate the application, you should call quit() on the QApplication. This will exit the application somewhat orderly. No need to rethrow exceptions. However, I would just call exit(0) and let the operating system handle clean up. I am not sure what advantage you gain by exiting your app by throwing exceptions compared to other methods of exiting the app.

                        1 Reply Last reply
                        3
                        • PerdrixP Perdrix

                          Its not there as part of expected flow its there to catch a totally invalid condition.

                          And the exception if caught by Qt MUST have been rethrown - how else could it end up being caught by the catch clause at the end of main.

                          In fact the code to which you pointed me DOES VERY CLEARLY rethrow the exception:

                          catch (...) {
                                  qWarning("Qt has caught an exception thrown from an event handler. Throwing\n"
                                           "exceptions from an event handler is not supported in Qt. You must\n"
                                           "reimplement QApplication::notify() and catch all exceptions there.\n");
                          
                                  // copied from below
                                  locker.relock();
                                  QEventLoop *eventLoop = d->threadData->eventLoops.pop();
                                  Q_ASSERT_X(eventLoop == this, "QEventLoop::exec()", "internal error");
                                  Q_UNUSED(eventLoop); // --release warning
                                  d->inExec = false;
                                  --d->threadData->loopLevel;
                          
                                  throw;
                              }
                          

                          THIS IS INTENDED TO TERMINATE THE APPLICATION WITH PREJUDICE

                          David

                          S Offline
                          S Offline
                          SamiV123
                          wrote on last edited by SamiV123
                          #11

                          @Perdrix said in Exceptions and Qt:

                          Its not there as part of expected flow its there to catch a totally invalid condition.

                          If we're talking about a BUG then that should be an assert (controlled abort) then. But you also said "recoverable" so that makes very little sense.

                          1 Reply Last reply
                          0
                          • aha_1980A Offline
                            aha_1980A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @Perdrix There has been a thread about Exceptions on the mailing list half a year ago: https://lists.qt-project.org/pipermail/development/2024-May/045252.html

                            You might get more insights about the problematics of Exceptions especially on different architectures from there.

                            Regards

                            Qt has to stay free or it will die.

                            1 Reply Last reply
                            0
                            • PerdrixP Offline
                              PerdrixP Offline
                              Perdrix
                              wrote on last edited by
                              #13

                              Sigh! This is an assertion. Some exceptions such as are thrown by std::filesystem are caught locally and never escape to Qt - those are the recoverable ones.

                              1 Reply Last reply
                              0
                              • PerdrixP Offline
                                PerdrixP Offline
                                Perdrix
                                wrote on last edited by
                                #14

                                @aha_1980 Oh my that's one scary thread on the mailing list. That suggests we need to replace all assertions with local code that detects the error and then terminates. At least we have the technology setup to implement that..

                                Now I at least understand something about the problem. Did anyone ever test -funwind?

                                David

                                1 Reply Last reply
                                0
                                • PerdrixP Perdrix has marked this topic as solved on

                                • Login

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