[[!meta title="Qt: Throw exceptions from signals and slots"]] [[!meta date="2010-07-08"]] [[!meta author="rohieb"]] [[!meta license="CC-BY-SA 3.0"]] By default, you can not throw exceptions from signals and slots, and if you try it, you get the following message: > Qt has caught an exception thrown from an event handler. Throwing > exceptions from an event handler is not supported in Qt. You must > reimplement QApplication::notify() and catch all exceptions there. So, what to do? The answer is simple: Overwrite the function `bool QApplication::notify(QObject * receiver, QEvent * event)` so that it catches all thrown exceptions. Here is some sample code: [[!format c """ #include #include class MyApplication : public QApplication { public: MyApplication(int& argc, char ** argv) : QApplication(argc, argv) { } virtual ~MyApplication() { } // reimplemented from QApplication so we can throw exceptions in slots virtual bool notify(QObject * receiver, QEvent * event) { try { return QApplication::notify(receiver, event); } catch(std::exception& e) { qCritical() << "Exception thrown:" << e.what(); } return false; } }; int main(int argc, char* argv[]) { MyApplication app(argc, argv); // ... } """]] Of course, you can also inherit from `QCoreApplication` to get rid of the `QtGui` dependency, or display a nice dialog box instead of printing the messages to the console, or… Found at: [Stack Overflow: Qt and error handling strategy][1] [1]: http://stackoverflow.com/questions/1578331/qt-and-error-handling-strategy [[!tag howto programming exceptions Qt signals slots]]