I'd like to do sth like this:
try
{
// ...
}
catch(const std::exception& ex)
{
// should preserve ex' runtime type
throw type_in_question(std::string("Custom message:") + ex.what());
}
Is that somehow possible without having to write a separate handler for each subtype?
What you're looking for would probably be something like this:
try {
// ...
}
template <typename Exc>
catch (Exc const& ex) {
throw Exc(std::string("Custom message:") + ex.what());
}
At least that's how we'd do such a thing in C++ typically. Unfortunatelly, you cannot write template code in a catch block like that. The best you could do is just add some runtime type information as a string:
try {
// ...
}
catch (std::exception const& ex) {
throw std::runtime_error(std::string("Custom message from ") +
typeid(ex).name() + ": " + ex.what());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With