Improve handling of uncaught exceptions by catching them early and displaying in a message box

This commit is contained in:
Bill Somerville 2018-11-20 23:43:18 +00:00
parent 6f966f613d
commit 179e093262

View File

@ -52,6 +52,38 @@ namespace
qsrand (seed); // this is good for rand() as well
}
} seeding;
// 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, translate ("main", "Fatal error"), e.what ());
throw;
}
catch (...)
{
MessageBox::critical_message (nullptr, translate ("main", "Unexpected fatal error"));
throw;
}
}
};
}
int main(int argc, char *argv[])
@ -68,7 +100,7 @@ int main(int argc, char *argv[])
// Multiple instances communicate with jt9 via this
QSharedMemory mem_jt9;
QApplication a(argc, argv);
ExceptionCatchingApplication a(argc, argv);
try
{
setlocale (LC_NUMERIC, "C"); // ensure number forms are in
@ -339,12 +371,10 @@ int main(int argc, char *argv[])
}
catch (std::exception const& e)
{
MessageBox::critical_message (nullptr, a.translate ("main", "Fatal error"), e.what ());
std::cerr << "Error: " << e.what () << '\n';
}
catch (...)
{
MessageBox::critical_message (nullptr, a.translate ("main", "Unexpected fatal error"));
std::cerr << "Unexpected fatal error\n";
throw; // hoping the runtime might tell us more about the exception
}