Is it unsafe to throw exceptions from statically linked C ++ libraries?

I heard that throwing exceptions from / from the C ++ library can be potentially dangerous, especially with a DLL, and especially if the calling code and library are compiled with different compilers. Is there any truth to this? Is it safe if I stick with static libraries? Please note that I am not talking about the internal use of exceptions only in the library, I also want to throw them in the calling code :)

Just to clarify: Let's say I got a compiled static library that defines the Foo class as follows:

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 it be safe?

+5
source share
4 answers

, ,

++, ABI. , , , MSVC, GCC.

.

:

MSVC , .

+3

GCC, , , , GCC , throw , "hidden" . GCC Visibility Wiki .

, DLL Windows , .

+3

Your example that you gave should work fine, but with a DLL, if you accidentally select an exception with a heap, you will crash if the consumer of the DLL tries to throw an exception thrown by the heap.

0
source

All Articles