2011-07-18 4 views
6

부스트 선물을 사용하고 has_exception()에 대한 미래의보고가 있으면 예외를 검색 할 수있는 방법이 있습니까? 예를 들어, 다음 코드는 다음과 같습니다.boost :: future에보고 된 예외를 얻는 방법?

int do_something() { 
    ... 
    throw some_exception(); 
    ... 
} 

... 

boost::packaged_task task(do_something); 
boost::unique_future<int> fi=task.get_future(); 
boost::thread thread(boost::move(task)); 
fi.wait(); 
if (fi.has_exception()) { 
    boost::rethrow_exception(?????); 
} 
... 

"?????"대신 무엇을 넣어야합니까?

+0

문서는 has_exception''말할'사실이이 비동기 결과와 관련된 * 경우, 그 결과는 검색을위한 준비가되어, 그 결과 저장된 예외 '입니다. 그러나 문서의이 위대한 비트는 그것을 얻는 방법을 말하지 않습니다 ... – CharlesB

+0

당신은 간단하게'fi.get()'시도 했습니까? – Nim

답변

7

http://groups.google.com/group/boost-list/browse_thread/thread/1340bf8190eec9d9?fwc=2에 따르면, 대신이 작업을 수행 할 필요가 :

#include <boost/throw_exception.hpp> 

int do_something() { 
    ... 
    BOOST_THROW_EXCEPTION(some_exception()); 
    ... 
} 

... 
try 
{ 
    boost::packaged_task task(do_something); 
    boost::unique_future<int> fi=task.get_future(); 
    boost::thread thread(boost::move(task)); 
    int answer = fi.get(); 
} 
catch(const some_exception&) 
{ cout<< "caught some_exception" << endl;} 
catch(const std::exception& err) 
{/*....*/} 
... 
+0

감사합니다. 그동안 소스 코드를 보면서 대답을 찾았습니다. 실제로, 나는 그것을 문서에서 충분히 잘 숨겨진 방법으로 발견했다. – petersohn

관련 문제