From e83b91de95edf356669fa3d547f03f8cd11b2ca4 Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Thu, 24 Sep 2020 18:32:58 +0100 Subject: [PATCH] Move ExceptionCatchingApplication class to its own header --- ExceptionCatchingApplication.hpp | 45 ++++++++++++++++++++++++++++++++ main.cpp | 36 ++----------------------- 2 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 ExceptionCatchingApplication.hpp diff --git a/ExceptionCatchingApplication.hpp b/ExceptionCatchingApplication.hpp new file mode 100644 index 000000000..657cca8c1 --- /dev/null +++ b/ExceptionCatchingApplication.hpp @@ -0,0 +1,45 @@ +#ifndef EXCEPTION_CATCHING_APPLICATION_HPP__ +#define EXCEPTION_CATCHING_APPLICATION_HPP__ + +#include + +#include "widgets/MessageBox.hpp" + +class QObject; +class QEvent; + +// +// We can't use the GUI after QApplication::exit() is called so +// uncaught exceptions can get lost on Windows systems where there is +// no console terminal, so here we override QApplication::notify() and +// wrap the base class call with a try block to catch and display +// exceptions in a message box. +// +class ExceptionCatchingApplication + : public QApplication +{ +public: + explicit ExceptionCatchingApplication (int& argc, char * * argv) + : QApplication {argc, argv} + { + } + bool notify (QObject * receiver, QEvent * e) override + { + try + { + return QApplication::notify (receiver, e); + } + catch (std::exception const& e) + { + MessageBox::critical_message (nullptr, "Fatal error", e.what ()); + throw; + } + catch (...) + { + MessageBox::critical_message (nullptr, "Unexpected fatal error"); + throw; + } + } +}; + +#endif diff --git a/main.cpp b/main.cpp index 116cb57ca..a19004d05 100644 --- a/main.cpp +++ b/main.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -28,6 +27,7 @@ #include #include +#include "ExceptionCatchingApplication.hpp" #include "Logger.hpp" #include "revision_utils.hpp" #include "MetaDataRegistry.hpp" @@ -63,38 +63,6 @@ namespace } } seeding; #endif - - // We can't use the GUI after QApplication::exit() is called so - // uncaught exceptions can get lost on Windows systems where there - // is no console terminal, so here we override - // QApplication::notify() and wrap the base class call with a try - // block to catch and display exceptions in a message box. - class ExceptionCatchingApplication final - : public QApplication - { - public: - explicit ExceptionCatchingApplication (int& argc, char * * argv) - : QApplication {argc, argv} - { - } - bool notify (QObject * receiver, QEvent * e) override - { - try - { - return QApplication::notify (receiver, e); - } - catch (std::exception const& e) - { - MessageBox::critical_message (nullptr, "Fatal error", e.what ()); - throw; - } - catch (...) - { - MessageBox::critical_message (nullptr, "Unexpected fatal error"); - throw; - } - } - }; } int main(int argc, char *argv[]) @@ -248,7 +216,7 @@ int main(int argc, char *argv[]) // Create a unique writeable temporary directory in a suitable location bool temp_ok {false}; - QString unique_directory {QApplication::applicationName ()}; + QString unique_directory {ExceptionCatchingApplication::applicationName ()}; do { if (!temp_dir.mkpath (unique_directory)