I am trying to understand how the exceptions can be propagated between different functions and returned to main function in C++. I have a small setup:
main.cpp:
int run () {
.
.
try {
testException(file);
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
test.cpp
std::exception_ptr g_exceptionPtr = nullptr;
void testThread() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
.
.
}
void testException(const std::string &file) {
TestCSV csv(file);
try {
std::thread input(testThread);
csv.writeToCSV(file, a, b, c);
input.join();
} catch (const std::runtime_error &e) {
g_exceptionPtr = std::current_exception();
std::rethrow_exception(g_exceptionPtr);
}
}
In test_csv.cpp:
TestCSV::writeToCSV(const std::string &file, const std::string &a, const std::string &b, const std::string &c) {
.
.
std::ofstream outFile(file);
if (!outFile.is_open()) {
throw std::runtime_error("Unable to open file for writing.");
}
}
Now I want to propagate the error from writeToCSV function and handle it in main.cpp. But, currently, this exception is caught in test.cpp but is not re-thrown to main.cpp.
What is the issue and how can I resolve it?
P.S: Above code is just an example and please let me know if any info is missing
You are calling
writeToCSV()in the context of the same thread that is callingrun(), so any exception thatwriteToCSV()throws and is not caught will already propagate torun()as expected. You don't need to do anything extra for that. Simply don'tcatchthe exception at all, or if you do then just re-throwit (notstd::rethrow_exception()). Your usage ofstd::threadis irrelevant in this situation.On the other hand, if your question is about propagating exceptions across threads, then you should change your test to reflect that. Moving
writeToCSV()into thestd::threadwould be a better test ofstd::current_exceptionandstd::rethrow_exception.std::current_exceptionallows you to capture a caught exception so you can access it outside of thecatch. You can capture the exception in the original thread that threw it, move it to the desired thread, and then callstd::rethrow_exceptionin that thread. In this case, after you havejoin'ed thestd::threadif theexception_ptrwas assigned, eg: