Qt class for catching exceptions (QExSafeApplication)

Posted on Posted in Qt Tutorials, Tutorials

Class below allows catching of Exceptions in Qt. Paste all code below into a header and use this class instead of QApplication in your main function.

 

#ifndef QEXSAFEAPPLICATION_H
#define QEXSAFEAPPLICATION_H
#include <QApplication>
#include <QtWidgets>

class QExSafeApplication : public QApplication
{
public:
QExSafeApplication(int& argc, char ** argv) :
QApplication(argc, argv) { }
virtual ~QExSafeApplication() { }

virtual bool notify(QObject *receiver, QEvent *event)
{
try
{
return QApplication::notify(receiver, event);
}
catch(std::exception& e)
{
QString msg;
msg.append(“Exception thrown : “);
msg.append(e.what());
QMessageBox::critical(0, “Error”, msg);
QMessageBox::critical(0, “Object”, receiver->objectName());
}
return false;
}

};

#endif // QEXSAFEAPPLICATION_H

Leave a Reply

Your email address will not be published. Required fields are marked *