I want to catch an exception throw from other shared library with specify type. The shared library is not link by the linker, but dynamic loaded by system API like dlopen, LoadLibrary, etc. And I manipulate the classes in the shared library by pure interface.
For example:
in a.so:
class IAException : public std::exception
{};
class IFoo
{
public:
virtual void foo() = 0;
};
class AException : IAException
{};
class Foo : public IFoo
{
public:
virtual void foo()
{
throw AException;
}
};
export "C"
{
IFoo* CreateFoo(){return new Foo;}
}
in another shared library b.so:
void* handle = dlopen("a.so", RTLD_NOW);
typedef IFoo* (CreateFooFunc)();
CreateFooFunc CreateFoo = (CreateFooFunc)dlsym(handle, "CreateFoo");
IFoo* foo = CreateFoo();
try
{
foo->foo();
}
catch(IAException& e)
{
// is it safe and cross-platfrom?
}
Can I catch the exact exception type in other shared library like this? I don't know whether the RTTI of exception is OK. How about I used on other platform like windows by LoadLibrary and visual c++? The behave is a c++ standard or not?
It is generally a bad idea to throw exceptions beyond the boundary of a library because you cannot be guaranteed that the library user will be able to catch the exceptions by exact type. This is because naming mangling in C++ is not standardized.
(Ok, you CAN be guaranteed if and only if the application that uses the library is compiled with the exact same compiler and compiler version as used to compile the shared library.)
I want to implement a plugin based program such as eclipse and netbeans in java. All modules just link a micro-core library which support basic plugin management, and put all logic into plugins.
I want each plugin can throw it's own exceptions, and in such a framework, I can guarantee the compiler.
The more I write, the more I find c++ is really not fit this architecture......