当异步函数抛出并且程序崩溃时,std :: future :: get()没有捕获异常

时间:2015-11-06 00:05:37

标签: c++ get std future

我试图从使用future::get()函数启动的函数中调用std::async函数。在我抛出的async函数中,我可以在future::get()调用中捕获异常。但是,我在get()调用中得到一个异常,该调用未在catch块中捕获,并且程序因unhandeld异常而崩溃。我错过了什么?

#include "stdafx.h"
#include <iostream>
#include <future>

void AsyncMethodThrowsExceptionTest();
void AsyncMethodThrowsException();

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    AsyncMethodThrowsExceptionTest();
    return 0;
}


void AsyncMethodThrowsExceptionTest()
{

    std::future<int> executionFuture;

    try
    {
        executionFuture = async(launch::async, AsyncMethodThrowsException);
    }
    catch(...)
    {
        cout << "caught ex [1]";
    }

    std::future_status::future_status status;

    status = executionFuture.wait_for(std::chrono::milliseconds(500u));
    if(status == std::future_status::deferred)
    {
        cout << "AsyncMethodThrowsException has not started";
    }
    else if(status == std::future_status::timeout)
    {
        cout << "AsyncMethodThrowsException timed out";
    }
    else if(status == std::future_status::ready)
    {
        cout << "AsyncMethodThrowsException successfully completed";
        try
        {
            if(executionFuture.valid())
            {
                executionFuture.get();
            }
        }
        catch(const std::future_error& ex)
        {
            cout << "AsyncMethodThrowsExceptionTest catch block";
        }
    }
}

void AsyncMethodThrowsException()
{
    throw(new exception("Exception from AsyncMethodThrowsException"));
}

1 个答案:

答案 0 :(得分:1)

你不仅在std::exception中抛出指向AsyncMethodThrowsException的指针(没有理由这样做),你们都捕获对异常而不是指针的引用,以及对那个std::exception的儿童班; std::future::get()抛出被调用函数抛出的确切异常,而不是std::future_error

还有其他一些语法问题:

  • std::future<int> executionFuture应为std::future<void> executionFuture
  • std::future_status::future_status status应为std::future_status status
  • std::exception没有带char const*std::string的构造函数,这可能是编译器扩展。

总结一下:

#include <iostream>
#include <future>

void AsyncMethodThrowsExceptionTest();
void AsyncMethodThrowsException();

using namespace std;

int main()
{
  AsyncMethodThrowsExceptionTest();
}


void AsyncMethodThrowsExceptionTest()
{
  std::future<void> executionFuture;

  try
  {
    executionFuture = async(launch::async, AsyncMethodThrowsException);
  }
  catch (...)
  {
    cout << "caught ex [1]";
  }

  std::future_status status = executionFuture.wait_for(std::chrono::milliseconds(500u));
  if (status == std::future_status::deferred)
  {
    cout << "AsyncMethodThrowsException has not started";
  }
  else if (status == std::future_status::timeout)
  {
    cout << "AsyncMethodThrowsException timed out";
  }
  else if (status == std::future_status::ready)
  {
    cout << "AsyncMethodThrowsException successfully completed";
    try
    {
      if(executionFuture.valid())
      {
        executionFuture.get();
      }
    }
    catch(const std::exception& ex)
    {
      cout << "AsyncMethodThrowsExceptionTest catch block";
    }
  }
}

void AsyncMethodThrowsException()
{
  throw runtime_error("Exception from AsyncMethodThrowsException");
}