gjb has asked for the wisdom of the Perl Monks concerning the following question:

Dear fellow Monks,

After doing a supersearch, a scan of all relevant docs and a grep on the .xs files of all the modules I've installed on my machine I didn't get a clue, so I'd like to seek your collective wisdom.

I'm currently writing a Perl module that interfaces to a C++ library. In several functions of this library, exceptions are thrown of a type I know, so in the XS code I use

try { ... } catch{MyException& e) { ... } catch(...) { ... }
to deal with it and to wrap the error message into a croak(...) for Perl consumption.

Upon testing it seems as if the C++ code throws the expected exception alright (this has been verified by the author of the C++ code), but in the XS code I can't catch that exception. However, something gets caught in the generic catch catch(...). I have turned on exception handling with the compiler option -GX.

Hence my question: has anyone successfully caught typed C++ exceptions in XS in this setup (NT 4.0 SP 5, ActiveState Perl 5.6.1 build 633, MSVC++ 6.0) or is there some advice you could offer?

Thanks in advance, -gjb-

Replies are listed 'Best First'.
Re: XS & exception handling
by Zaxo (Archbishop) on Jan 09, 2003 at 13:32 UTC

    In gcc you can compile with the -fexceptions flag, which permits the C code to pass C++ exceptions along. That ability is not portable.

    Your best bet is to produce a C/C++ interface to the C++ library. Use extern "C" linkage qualifier, and the throw() exception specifier. Catch all exceptions and handle them by setting errno appropriately and returning an error flagging result like PL_sv_undef.

    I haven't tested the C++ compliance of MSVC6, but there is some chance that exception handling will be adequate.

    After Compline,
    Zaxo

Re: XS & exception handling (C vs. C++)
by tye (Sage) on Jan 09, 2003 at 18:36 UTC

    I think Zaxo hit on the problem. Your *.xs code is compiled as C code not as C++ code.

    You can get XS to compile your code as C++ code. The last time I did this was quite some time ago so it required some hacking but wasn't very hard. I think that it is easy in modern versions of XS (like a "-C++" flag on the command line).

                    - tye
Re: XS & exception handling
by broquaint (Abbot) on Jan 10, 2003 at 14:50 UTC
Re: XS & exception handling (pinpointed)
by gjb (Vicar) on Jan 13, 2003 at 09:57 UTC

    The problem has finally been pinpointed, thanks Zaxo, tye and broquaint for their suggestions.

    It turned out that the problem was in the library's C++ code. Internally, exceptions are caught using

    try {...} catch(...) {...}
    In the catch, an error code is set, which is subsequently tested in the calling code (via a macro expansion) so that an exception is thrown (just don't ask, it's not my code). The problem is that in the original catch some logging is done which... yeah, right, generates an error and hence some exception is thrown that only got caught in the catch(...) of my XS code, completely bypassing the intended exception throwing/catching mechanism.

    Lessons learned:

    • exceptions are handled as one would expect in this setup (NT, ActiveState Perl, MSVC++),
    • never ever trust someone who says he tested his code (his test may be succesful, but simply not adequate to find the real problem).

    Thanks again to all who helped, -gjb-