【发布时间】:2009-03-20 17:08:53
【问题描述】:
基本上,我遇到了一个线程抛出一个不同线程需要处理的异常的情况。我正在尝试使用 boost 异常来执行此操作,但是在某处异常会丢失其类型,因此不会被 catch 块捕获。
基本上,线程 B 想要做某事,但是由于各种原因,它必须与线程 A 一起完成(如果您想知道这些原因,请询问 MS 为什么必须由同一线程创建、重置和释放 Direct3d 9 设备)创建了窗口)。如果在执行这些操作时发生异常,线程 A 会捕获它,并将其传递回线程 B,然后根据需要重新抛出它以进行处理。问题是线程 B 中抛出的异常似乎与线程 A 中抛出的异常不同。:(
我的程序的调试输出,代码如下。
0x776b42eb 处的第一次机会异常 ...: fllib::exception::Error at memory location 0x0019e590.. 0x776b42eb 处的第一次机会异常 ...:在内存位置 0x00000000 处 [rethrow].. 0x776b42eb 处的第一次机会异常 ...: boost::exception_detail::clone_impl<:unknown_exception> 在内存位置 0x0019eed4..//thread B
...
try
{
SendCallback(hwnd, boost::bind(&Graphics::create, this));
}
catch(fllib::exception::Error &except)//example catch block, doesnt catch example exception
{
...handle exception...
}
void SendCallback(HWND hwnd, boost::function<void()> call)
{
boost::exception_ptr *except_ptr =
(boost::exception_ptr*)SendMessage(hwnd, WM_CALLBACK, (unsigned)&call, SEND);
if(except_ptr)//if an exception occurred, throw it in thread B's context
{
boost::exception_ptr except = *except_ptr;
delete except_ptr;
boost::rethrow_exception(except);
}
}
//thread A
LRESULT CALLBACK HookProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
//check for our custom message
if(msg == WM_CALLBACK)
{
if(lParam == POST)
{
...
}
else
{
boost::function<void()> *call = (boost::function<void()>*)wParam;
try
{
(*call)();
}
catch(...)
{
return (unsigned)new boost::exception_ptr(boost::current_exception());
}
return 0;
}
}
else
{
...
}
}
void Graphics::create()
{
...code that may throw exceptions...
eg
throw fllib::exception::Error(L"Test Exception...");
}
【问题讨论】:
-
@Neil Butterworth 为什么要删除部分 VS 调试输出????
-
抱歉 - 我以为我刚刚更正了标题。
-
throw ... 在我提供的调试输出的情况下: throw fllib::exception::Error(L"Test exception...");
-
可能需要尝试使用 throw boost::enable_current_exception(my_exception()); revergestudios.com/boost-exception/…
-
好的,除了我不控制所有可以在该块中引发异常的代码......然后唯一的选择是创建大量的catch块来“转换”异常,例如catch( ExceptionType &except){boost::throw_exception(except);}catch(AnotherException &except){...
标签: c++ multithreading exception boost exception-handling