mercredi 25 janvier 2017

What happens to exceptions thrown from functions launched via std::async

What happens to exceptions thrown from functions launched via std::async?

#include <future>
#include <iostream>
#include <stdexcept>

void foo()
{
  std::cout << "foo()" << std::endl;
  throw std::runtime_error("Error");
}

int main()
{
  try
  {
    std::cout << "1" << std::endl;
    auto f = std::async(std::launch::async, foo);
    f.get();
    std::cout << "2" << std::endl;
  }
  catch (const std::exception& ex)
  {
    std::cerr << ex.what() << std::endl;
  }
}

MSVC gives me the following output:

1
foo()
Error

Is it right that such exceptions will be caught outside the function by calling the get function?

Aucun commentaire:

Enregistrer un commentaire