Discussion:
Auto-inserting wrappers to handle C++ exceptions
(too old to reply)
Olly Betts
2007-12-06 15:08:09 UTC
Permalink
I'm wrapping a C++ library, and wanting to add something like this
around (almost) every method:

try {

[code which calls the method]

} catch (...) {
convert_to_a_perl_exception();
}

And then convert_to_a_perl_exception() rethrows and recatches the
exception to find what type it is and so how to convert it to a Perl
exception.

Is there a way to insert a standard wrapper like this around every XS
wrapped method in a .xs file? Bonus points if certain methods can be
excluded (ones which are documented to never throw exceptions), although
there aren't many of those, so wrapping everything is acceptable.

Looking at the documentation, I can see how I can specify it for each
method using INIT and CLEANUP, but I don't see how to specify it just
once per file.

My best idea so far is to define C macros for the two chunks of
exception handling code, so at least I only have to insert four standard
lines for each method which requires exception handling. Something
like:

#define TRY try {
#define CATCH } catch (...) { convert_to_a_perl_exception(); }

[...]

INIT:
TRY
CLEANUP:
CATCH

Cheers,
Olly
Nicholas Clark
2007-12-14 16:33:01 UTC
Permalink
Post by Olly Betts
I'm wrapping a C++ library, and wanting to add something like this
try {
[code which calls the method]
} catch (...) {
convert_to_a_perl_exception();
}
And then convert_to_a_perl_exception() rethrows and recatches the
exception to find what type it is and so how to convert it to a Perl
exception.
Is there a way to insert a standard wrapper like this around every XS
wrapped method in a .xs file? Bonus points if certain methods can be
excluded (ones which are documented to never throw exceptions), although
there aren't many of those, so wrapping everything is acceptable.
I had hoped that there would be an easy way to do this, and that someone
would know it.
Post by Olly Betts
Looking at the documentation, I can see how I can specify it for each
method using INIT and CLEANUP, but I don't see how to specify it just
once per file.
I wondered if not, whether it would be possible to override the XS code
generator to do this for every function, but even though it's now extracted
out from xsubpp into ExtUtils::ParseXS, there's no easy way to get the
new thin xsubpp to call into a different parser, is there?
Post by Olly Betts
My best idea so far is to define C macros for the two chunks of
exception handling code, so at least I only have to insert four standard
lines for each method which requires exception handling. Something
#define TRY try {
#define CATCH } catch (...) { convert_to_a_perl_exception(); }
[...]
TRY
CATCH
I don't have any better ideas than this, I'm afraid.

Nicholas Clark

Loading...