Qt class for catching exceptions (QExSafeApplication)

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


2 Replies to “Qt class for catching exceptions (QExSafeApplication)”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.