Sign up for the KDAB Newsletter
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Find what you need - explore our website and developer resources
10 May 2023
In this video, I'm interviewing Giuseppe D'Angelo to check up with one of my fundamental understandings, which says: "you should never use exception handling in Qt."
What I learned was: "It depends..." - Watch this video to learn what you can do and what you shouldn't do.
Qt exception safety policy is documented here https://doc.qt.io/qt-6/exceptionsafety.html , a page that very few people know about.
That first sentence says it all: "Preliminary warning: Exception safety is not feature complete! Common cases should work, but classes might still leak or even crash." However, it also offers some extra information.
Now, to really nitpick: Qt containers try to honor some form of exception safety, and there are some autotests. However the tests are not as thorough as they should be; we keep finding places where exception safety is broken (e.g. https://codereview.qt-project.org/c/qt/qtbase/+/459611 fixed a 6-year-old bug in the area). We basically pay the price for the fact that these codepaths are almost never exercised at large.
At the end, the page says that throwing an exception through signals and slots is undefined behavior. This is a way for Qt to tell users to not ever rely on a behavior that "seems to work in practice" (even if it does work today, it may break in the future, and Qt won't accept it as a bug).
00:00 Intro
01:50 Why doesn't Qt use exceptions?
04:25 Exceptions in my own code
06:30 Throwing exceptions through Qt code
08:00 Qt containers are not exception-safe
15:40 Exceptions thrown through Qt signals and slots
21:28 Exceptions thrown through Qt's event loop
25:45 Summary