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 itself will not throw exceptions. Instead, error codes are used. In addition, some classes have user visible error messages, for example QIODevice::errorString() or QSqlQuery::lastError(). This has historical and practical reasons - turning on exceptions can increase the library size by over 20%.
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. If that is not possible, in Qt 5 you must at least re-implement QCoreApplication::notify() and catch all exceptions there.
An exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another. VB.Net exception handling is built upon four keywords - Try, Catch, Finally and Throw.
We do this by using the Code property of the SoapException object. The Code property will be set to Client if the exception is caused by an invalid input from the client. If the exception is caused by the Web service code (for example, the database server is down), the Code property will be set to Server.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With