Skip to content

Commit 17ad16c

Browse files
committed
app: print more errors without std::terminate
1 parent 98ac944 commit 17ad16c

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

src/noggit/application.cpp

+53-19
Original file line numberDiff line numberDiff line change
@@ -295,22 +295,23 @@ Noggit::Noggit(int argc, char *argv[])
295295

296296
namespace
297297
{
298-
void noggit_terminate_handler()
298+
std::string current_exception_string()
299299
{
300-
std::string const reason
301-
{util::exception_to_string (std::current_exception())};
302-
303-
if (qApp)
304-
{
305-
QMessageBox::critical ( nullptr
306-
, "std::terminate"
307-
, QString::fromStdString (reason)
308-
, QMessageBox::Close
309-
, QMessageBox::Close
310-
);
311-
}
300+
return util::exception_to_string (std::current_exception());
301+
}
312302

313-
LogError << "std::terminate: " << reason << std::endl;
303+
void current_exception_messagebox (QString title)
304+
{
305+
QMessageBox::critical ( nullptr
306+
, title
307+
, QString::fromStdString (current_exception_string())
308+
, QMessageBox::Close
309+
, QMessageBox::Close
310+
);
311+
}
312+
void current_exception_log (std::string title)
313+
{
314+
LogError << title << ": " << current_exception_string() << std::endl;
314315
}
315316

316317
struct application_with_exception_printer_on_notify : QApplication
@@ -325,22 +326,55 @@ namespace
325326
}
326327
catch (...)
327328
{
328-
std::terminate();
329+
current_exception_messagebox ("event loop exception");
330+
current_exception_log ("event loop exception");
331+
332+
exit (1);
333+
334+
return false;
329335
}
330336
}
331337
};
332338
}
333339

334340
int main(int argc, char *argv[])
341+
try
335342
{
336343
noggit::RegisterErrorHandlers();
337-
std::set_terminate (noggit_terminate_handler);
344+
std::set_terminate
345+
( []
346+
{
347+
if (qApp)
348+
{
349+
current_exception_messagebox ("std::terminate");
350+
}
338351

339-
QApplication qapp (argc, argv);
352+
current_exception_log ("std::terminate");
353+
}
354+
);
355+
356+
application_with_exception_printer_on_notify qapp (argc, argv);
340357
qapp.setApplicationName ("Noggit");
341358
qapp.setOrganizationName ("Noggit");
342359

343-
Noggit app (argc, argv);
360+
//! \note Extra level here because we won't have a qapp below,
361+
//! but we still want to not std::terminate if throwing above either.
362+
try
363+
{
364+
Noggit app (argc, argv);
365+
366+
return qapp.exec();
367+
}
368+
catch (...)
369+
{
370+
current_exception_messagebox ("top level exception");
371+
372+
throw;
373+
}
374+
}
375+
catch (...)
376+
{
377+
current_exception_log ("top level exception");
344378

345-
return qapp.exec();
379+
return 1;
346380
}

0 commit comments

Comments
 (0)