捕获加载的共享库中抛出的异常是否可移植?
我注意到它适用于 dlfcn.h
,但我想知道这种行为是否通常是预期的,例如当在 Windows 上使用 LoadLibrary
时?
示例代码:
main.cpp:
#include <stdexcept>
#include <cstdio>
#include <dlfcn.h>
typedef void(*dummy_t)();
int main()
{
dummy_t f;
void* handle;
handle = dlopen("module.so", RTLD_LAZY);
f = (dummy_t)dlsym(handle, "modulemain");
try
{
f();
}
catch(std::runtime_error& e)
{
fprintf(stderr, "caught exception: %s\n", e.what());
}
dlclose(handle);
}
module.cpp:
#include <stdexcept>
extern "C" void modulemain()
{
throw std::runtime_error("some terrible error occured");
}
最佳答案
是的,在 Windows 下应该可以正常工作。
关于c++ - 在加载的共享库中捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5622867/