From 6158665aeb03840ed9529269e2b27d5ca7fab1a5 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 15 Nov 2024 03:09:00 -0800 Subject: [PATCH] Don't rely on terminate in a catch block giving a useful message --- common/alassert.cpp | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/common/alassert.cpp b/common/alassert.cpp index a6908cc5..aa44841e 100644 --- a/common/alassert.cpp +++ b/common/alassert.cpp @@ -1,38 +1,44 @@ #include "alassert.h" -#include #include #include +namespace { + +[[noreturn]] +void throw_error(const std::string &message) +{ + throw std::runtime_error{message}; +} + +} /* namespace */ + namespace al { [[noreturn]] void do_assert(const char *message, int linenum, const char *filename, const char *funcname) noexcept { - /* Calling std::terminate in a catch block hopefully causes the system to - * provide info about the caught exception in the error dialog. At least on - * Linux, this results in the process printing + /* Throwing an exception that tries to leave a noexcept function will + * hopefully cause the system to provide info about the caught exception in + * an error dialog. At least on Linux, this results in the process printing * * terminate called after throwing an instance of 'std::runtime_error' * what(): * * before terminating from a SIGABRT. Hopefully Windows and Mac will do the - * appropriate things with the message for an abnormal termination. + * appropriate things with the message to alert the user about an abnormal + * termination. */ - try { - std::string errstr{filename}; - errstr += ':'; - errstr += std::to_string(linenum); - errstr += ": "; - errstr += funcname; - errstr += ": "; - errstr += message; - throw std::runtime_error{errstr}; - } - catch(...) { - std::terminate(); - } + auto errstr = std::string{filename}; + errstr += ':'; + errstr += std::to_string(linenum); + errstr += ": "; + errstr += funcname; + errstr += ": "; + errstr += message; + + throw_error(errstr); } } /* namespace al */ -- 2.11.4.GIT