I’ve heard that throwing exceptions in/from a C++ library could be potentially dangerous, particularly with DLLs, and particularly if the calling code and the library are compiled with different compilers. Is there any truth to this? Is it safe as long as I stick to static libraries? Note that I am not talking about internal use of exceptions in the library only, I want to throw them deep into the calling code as well 🙂
Just to clarify: Say I got a compiled static library that defines class Foo like this:
class Foo
{
public:
// Constructor
Foo()
{
/* ... Do stuff ... */
if (stuffwentwrong)
throw(123); // We throw an integer error code (to make it simple)
}
};
And some guy uses it like this:
try
{
Foo foo_object;
}
catch (int i)
{
std::cout << "Oh bum. Code: " << i;
}
Would that be safe?
You generally can’t mix different C++ compilers that do not have compatible ABI. So, for example, you can’t throw exception from library compiled with MSVC and try to catch
it with GCC.
But otherwise, you generally have no issues.
Small note:
MSVC has several incompatible exception models, don’t mix them.