Skip to content

Commit

Permalink
feat: display exception name when unhandled exception got thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
shenlebantongying authored and xiaoyifang committed Apr 10, 2023
1 parent 21fa719 commit 880bf48
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions termination.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@

static void termHandler()
{
QString message( "GoldenDict has crashed with an unexpected exception\n\n" );
qDebug() << message;

// Trick: In c++17, there is no standardized way to know the exception name/type inside terminate_handler
// So, we just get the exception and throw it again, so that we can call the std::exception::what :)

// This trick can be removed/improved if something similar to
// boost::current_exception_diagnostic_information got standardized,

std::exception_ptr curException = std::current_exception();

try {
if ( curException != nullptr ) {
std::rethrow_exception( curException );
}
else {
qDebug() << "GoldenDict has crashed unexpectedly.\n\n";
}
}
catch ( const std::exception & e ) {
qDebug() << "GoldenDict has crashed with exception: " << e.what() ;
}

if( logFilePtr && logFilePtr->isOpen() )
logFilePtr->close();
Expand Down

0 comments on commit 880bf48

Please sign in to comment.