繁体   English   中英

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

[英]std::future::get() does not catch the exception when the async function throws and program crashes

我试图从使用std::async函数启动的函数调用future::get()函数。 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"));
}

你不仅在AsyncMethodThrowsException抛出一个指向std::exceptionAsyncMethodThrowsException (没有理由这样做),你们都捕获对异常的引用而不是指针,以及对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的构造char const* ,这可能是编译器扩展。

总结一下:

#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");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM