I'm trying to catch an exception of a thread from outside but I can't, here is my code:
#include <iostream>
#include <thread>
#include <chrono>
void func1()
{
while (true)
{
std::cout<<"thread 1"<<std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}
}
void func2()
{
int i = 0;
while (true)
{
std::cout<<"thread 2"<<std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
if (i++ == 3)
{
throw 1;
}
}
}
int main()
{
try{
std::thread t1(func1);
std::thread t2(func2);
t2.join();
t1.join();
}catch(int e){
std::cout<<"exception catched!"<<std::endl;
}
std::cout<<"main end"<<std::endl;
return 0;
}
However, I get a core dumped immediately, it doesn't seem that I caught the exception:
thread 1
thread 2
thread 1
thread 2
thread 1
thread 2
thread 1
thread 2
thread 1
terminate called after throwing an instance of 'int'
Aborted (core dumped)
Aucun commentaire:
Enregistrer un commentaire