xsubpp can generate exception handling code for c/c++ files converted from .xs files. It generates the following piece of code for me
TRY {
char * CLASS = (char *)SvPV_nolen(ST(0));
Example * RETVAL;
RETVAL = new Example();
ST(0) = sv_newmortal();
sv_setref_pv( ST(0), CLASS, (void*)RETVAL );
}
BEGHANDLERS
CATCHALL
sprintf(errbuf, "%s: %s\tpropagated", Xname, Xreason);
ENDHANDLERS
But when compiling the generated code, I’m getting compilation errors as TRY, BEGHANDLERS, CATCHALL, ENDHANDLERS were not defined anywhere in perl header files. I’ve modified my code to define the above mentioned tokens like this.
#define TRY try
#define BEGHANDLERS
#define CATCHALL catch (...) {
#define ENDHANDLERS }
But I’m not able to give meaningful definitions to Xname and Xreason. Are the above definitions correct ? How do we handle the above mentioned keywords
This falls into the “well don’t do that then” category. (The canonical answer to “Doctor, it hurts when I do X”). Look at the generated code:
This isn’t propagating the exception. It is printing a message and then utterly ignoring the fact that an error occurred!
Perl’s support for C++ is rather weak. That shouldn’t be that surprising; perl was written in C and targets C for its external subroutines.
My suggestion: Do handle exceptions, but do not use that rather klunky exception stuff provided for ‘free’ from
xsubpp. Instead write thetry ... catch ...block yourself. Make thecatchblock convert those caught C++ exceptions to perl exceptions. CallPerl_croakfor fatal errors,Perl_warnfor non-fatal errors.And good luck. Interfacing perl to C/C++ is not easy.
Some potentially helpful links: