An unused exception behaves differently for the main thread and another std :: thread.
here is the test program
#include <thread> class XXX{ public: XXX(){std::fprintf(stderr, "XXX ctor\n");} ~XXX(){std::fprintf(stderr, "XXX dtor\n");} }; void mytest(int i) { XXX xtemp; throw std::runtime_error("Hello, world!"); } int main(int argc, char *argv[]) { if(argc == 1) { mytest(0); }else{ std::thread th([&]{mytest(0);}); th.join(); } }
above code (C ++ 11) compiled by GCC 5.4 to work without arguments
XXX ctor terminate called after throwing an instance of 'std::runtime_error' what(): Hello, world! Aborted (core dumped)
run 1 arg:
XXX ctor XXX dtor terminate called after throwing an instance of 'std::runtime_error' what(): Hello, world! Aborted (core dumped)
So, stack unwinding is done in the worker thread, but not in the main thread, WHY?
I ask because I would like the kernel to provide useful information about the reverse stack in both cases (for an uncaught exception).
Thanks in advance!
Further research shows that the noexcept keyword adds the noexcept keyword to the body of the stream function. mytest () can partially solve my problem because unwinding will fail, but this is not a good solution, because recovery will be partially partial if mytest () calls another function without any guarantees and actually throws an uncaught exception.
Update: thanks to all comment providers, now I understand that the C ++ exception does not support feedback, and GCC, as a C ++ implementation, has the right to choose not to untie when the excluded exception is selected from the main thread and unwinds when from the workflow.
Update: Special thanks to Sid S and Jive Dadson, I have to confuse some concepts: 1) exception / error handling; 2) assertion of the runtime 3) Segment error, 2 and 3 are similar, they are UN recovery errors, interruption is immediately the only choice, they are also debugger friendly because the package is not involved. they should not be implemented using the concept of exceptions in general. the exception is always supposed to be caught, although the exception of the excluded exception from main () is not recommended.