Exceptions and Qt
-
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
-
@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
-
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
-
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
-
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
-
Qt rethrew the exception AFAICT as it was reported by the catch code at the end of main() before termination
D. -
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.
-
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
@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.
-
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
-
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
@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 callexit(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. -
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
@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.
-
@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
-
@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
-
P Perdrix has marked this topic as solved on