Say I want to create an exception object, but throw it a bit later. My use case is that the exception is generated in a thread, but I need to actually throw it from my main thread (because there's nobody to catch it in the child thread)
using namespace std;
runtime_error *ex = nullptr;
thread myThread( [&ex]{
if (something_went_wrong) {
ex = new runtime_error("Something went wrong");
return;
}
} );
myThread.join();
if (ex != nullptr) {
throw *ex;
}
For what it's worth, the compiler seems happy with this, but I can see that it's a resource leak at best. Is there a right way to do this?