exception: Add exception_msg helper

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2025-05-13 09:36:22 +02:00
parent 4c499f2905
commit be273d4263
2 changed files with 18 additions and 1 deletions

View File

@ -39,7 +39,7 @@ int main(int, const char *[])
return 0;
}
catch (const std::exception& ex) {
std::cerr << ex.what() << " FAIL\n";
std::cerr << stdex::exception_msg(ex) << " FAIL\n";
return 1;
}
}

View File

@ -23,4 +23,21 @@ namespace stdex
///
user_cancelled(_In_opt_z_ const char* msg = "operation cancelled") : runtime_error(msg) {}
};
///
/// Concatenates error messages when exception is nested.
///
/// \param ex Exception
///
/// \returns Concatenated exception messages with ": " delimiter.
///
inline std::string exception_msg(const std::exception& ex)
{
try { std::rethrow_if_nested(ex); }
catch (const std::exception& nested_ex) {
return std::string(ex.what()) + ": " + exception_msg(nested_ex);
}
catch (...) {}
return ex.what();
}
}