Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Qt use exception handling?

Tags:

qt

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?

like image 755
Rohan Prabhu Avatar asked Apr 05 '11 08:04

Rohan Prabhu


People also ask

Does Qt use exceptions?

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%.

How do you handle exceptions in Qt?

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.

What is exception handling in Visual Studio?

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.

How do you handle exceptions in Web services?

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.


2 Answers

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.

like image 187
Frank Osterfeld Avatar answered Oct 11 '22 03:10

Frank Osterfeld


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.

like image 26
hmuelner Avatar answered Oct 11 '22 03:10

hmuelner