У меня есть функция throwing:
void calculateValuesThrowing(std::promise<int>&& pr, int a, int b)
{
try
{
auto timeout = std::chrono::seconds(5);
logInfo("Calculating value...");
std::this_thread::sleep_for(timeout);
if (a == b)
{
throw std::runtime_error("a cannot equal b");
}
pr.set_value(a + b);
}
catch(std::exception& exc)
{
pr.set_exception(std::current_exception()); // ok, jump here right after the "throw" above
}
}
, и я называю это так:
где-то в функции main ():
try
{
std::promise<int> promise;
auto future = promise.get_future();
std::thread th(calculateValuesThrowing, std::move(promise), 10, 10);
auto result = future.get();
th.join();
}
catch(std::exception& exc)
{
std::cout << "Error:" << exc.what(); // never get there
}
Я ожидаю исключение convertValuesThrowing будет "переброшено", чтобы я мог обработать его в функции catch (), но сразу после того, как convertValuesThrowing завершит работу, я получу abort () .
Что я делаю не так?