Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ problem: exception not caught

Tags:

c++

exception

g++

The situation is that I have a dynamic library written in C++ that is wrapped for Python by another dynamic library, also written in C++ (generated by SIP to be specific). The first dynamic library defines a function do_raise, which throws an exception RaiserError, a subclass of std::exception. The second dynamic library, in wrapping do_raise, tries to catch RaiserError in order to translate it to a Python exception.

Building with Visual C++, everything works as expected and RaiserError is caught correctly. Using g++ on Linux however, RaiserError is not caught. If I try to catch std::exception instead (RaiserError's baseclass), it works. What goes wrong here? Do the two libraries have different notions of the RaiserError type, since it does not get recognized by the catch block?

For testing I also write a small executable which calls do_raise in the C++ library, and here I am able to catch RaiserError even with g++.


2 Answers

You can't reliably pass C++ exceptions across module boundaries. On Windows (at least for 32-bit processes) there is a standard ABI for that, but with gcc, you're out of luck.

like image 89
avakar Avatar answered Jan 31 '26 04:01

avakar


I believe that GCC uses some kind of pointer to the type information and if it does not match the pointer, then the types are not the same.

This sounds like one of the cases where Microsoft C++ falls back to string comparisons of type information (slow but works). That will not help you with GCC.

You might try making sure that the exception class is not hidden and that the typeinfo is visible in the ELF .so file. Try to make sure that the type is defined in only one .so.

Edit: I just thought of this: I believe ELF dynamic linkers are just fine with multiple copies of the typeinfo symbols and will choose a single copy to use. I think you can get multiple copies though if you linked some static code or did -Bsymbolic in one of the .so or executable files.

like image 30
Zan Lynx Avatar answered Jan 31 '26 02:01

Zan Lynx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!