Is it safe to pass c_str()
as a parameter when constructing std::exception
? Please let me know if handling exceptions like this is a bad idea. In my project all error messages are returned from a function as std::string
and then thrown as a std::exception
.
#include <iostream>
int main()
{
try {
std::string message="! Something went wrong.";
throw std::exception(message.c_str());
}
catch (std::exception ex) {
std::cerr << ex.what() << std::endl;
}
}