为什么程序在堆栈展开后无法达到正确的返回指令



编译器:g++9.2.0
操作系统:Windows 10
g++调用:

g++ -E main.cpp -v -o main.i
g++ -c main.cpp -v -o main.o 
g++ main.o -v -o main.exe
main.exe

main.cpp:

#include <chrono>
#include <iostream>
#include <string>
#include <exception>
#include <iostream>
//#include <thread>
#include "mingw.thread.h"
struct Object{
struct Exception : public std::exception{
std::string error_;
Exception(std::string str){
this->error_ = str;
}
~Exception() {
}
std::string get(){
return error_;
}
};
void DoSomeWork() {
try {
std::thread AnotherTh(&Object::GenerateException ,this);
AnotherTh.detach ();
while(true);
}
catch (...) {
throw ;
}
}
void GenerateException(){
std::this_thread::sleep_for (std::chrono::seconds(5));
throw Object::Exception ("Some error");
}
};
int main(){
try{
Object instance;
std::thread th(&Object::DoSomeWork,std::ref(instance));
th.join ();
}
catch (Object::Exception &ex ) {
std::cout << ex.get ();
}
catch (std::exception &ex ){
std::cout << ex.what ();
}
catch (...){
}
std::cout << "never reach this";
return 0;
}

输出:

terminate called after throwing an instance of 'Object::Exception'
what():  std::exception

我正在启动带有新线程(th(主线程并等待它,在th内部启动

另一个将抛出异常的线程我不明白为什么它不能处理异常并将其传递给main的try-catch。

为什么程序在堆栈展开c++后无法到达正确的返回指令?

因为您的代码创建了多个线程,而您没有在实际引发异常的线程中捕获异常。即使调用std::threadjoin()成员函数,异常也不会在线程之间传播。

Try块被定义为堆栈的动态构造。try块捕获通过调用从其内容动态访问的代码引发的异常。

当您创建一个新线程时,您将创建一个全新的堆栈,该堆栈根本不属于try块的动态上下文的一部分,即使对pthread_create或可构造联接的std::thread()的调用在try中也是如此。

要捕获源于线程X的异常,必须在线程X中使用try-catch子句(例如,围绕线程函数中的所有内容,类似于您在main中已经做的操作(。

有关相关问题,请参阅如何在线程之间传播异常?。

一个例子:

#include <chrono>
#include <iostream>
#include <string>
#include <exception>
#include <iostream>
#include <thread>

struct Object {
void DoSomeWork() 
{
std::cout << "DoSomeWork Thread ID: " << std::this_thread::get_id() << std::endl;
try {
std::thread thread(&Object::GenerateException, this);
thread.detach();
while(true);
}
catch (...) {
std::cout << "Caught exception: " << std::this_thread::get_id() << std::endl;
throw ;
}
}
void GenerateException(void)
{
std::cout << "GenerateException Thread ID: " << std::this_thread::get_id() << std::endl;
try {
std::this_thread::sleep_for (std::chrono::seconds(5));
throw std::runtime_error("Some error");
} catch (...) {
std::cout << "Caught exception: " << std::this_thread::get_id() << std::endl;
throw;
}
}
};
int main()
{
std::cout << "Main Thread ID: " << std::this_thread::get_id() << std::endl;
try {
Object instance;
std::thread th(&Object::DoSomeWork,std::ref(instance));
th.join();
}
catch (const std::exception &ex) {
std::cout << ex.what() << std::endl;
std::cout << "Exception caught at: " << std::this_thread::get_id() << std::endl;
}
std::cout << "never reach this" << std::endl;
return 0;
}

输出:

Main Thread ID: 140596684195648
DoSomeWork Thread ID: 140596665124608
GenerateException Thread ID: 140596656670464
Caught exception: 140596656670464
terminate called after throwing an instance of 'std::runtime_error'
what():  Some error
Aborted (core dumped)

从此std::thread参考:

。。。如果它通过抛出异常而终止,则调用std::terminate

如果在线程中引发未捕获的异常,则程序将被强制终止。

最新更新