Why doesn't Qt use exception handling?
QtQt Problem Overview
I have always wondered that since Qt uses almost every C++ feature in the standard and in a wonderful and an innovative way, and whenever it doesn't use a particular feature, it has a perfectly valid and applicable reason for not doing so. In that case, why is it that no Qt classes ever use the throw
keyword and throw
any exceptions? What is their rationale behind never requiring a try
...
catch
block in our Qt written code?
Personally, I myself don't like it much and never use it whether or not I'm working with Qt and always go with error codes and return values instead of throwing exception objects. But why don't I see a QException class in their documentation? What is the Qt developers viewpoint on this?
Qt Solutions
Solution 1 - Qt
For historic reasons, mostly. Exception support in compilers took quite some time to mature. Citing Nokia's Tobias Hunger:
> "When Qt was started exceptions were not available for all the compilers that needed to be supported by Qt. Today we are trying to keep the APIs consistent, so modules that have a history of not using exceptions will generally not get new code using exceptions added. You will notice exceptions are used in some of the new modules of Qt."
I think that sums it up pretty much.
Solution 2 - Qt
If you google for "qt exceptions" you will get lot of discussions about this topic. Here is an "official" answer:
> When Qt was started exceptions were not available for all the compilers that needed to be supported by Qt. Today we are trying to keep the APIs consistent, so modules that have a history of not using exceptions will generally not get new code using exceptions added. > > You will notice exceptions are used in some of the new modules of Qt.
If you look for exception in the index of assistant (i.e. in the Qt documentation) you will find some exception classes, e.g. QtConcurrent::Exception.
Solution 3 - Qt
You can read a nice, mostly civilized debate about exceptions here on the KDE devel mailinglist. Since KDE and QT are related I assume the same issues apply, which (if I read the thread correctly) can be summarized as:
- Exceptions have performance issues depending on compiler.
- Shielding users of the library from the obligation to use exceptions (by not throwing them from the library)
- Issues around the spec for exceptions in C++