Search code examples
c++perlexceptionxs

Prevent C++ exception from crashing my Perl script


I have a Perl script that uses some C++ library which is probably wrapped using xs. When the library throws an exception it crashes my Perl script even when called from inside eval. How can I skip the exception and prevent my script from crashing?

terminate called after throwing an instance of 'blaException'
  what():  blablabla.cpp:202: Failed to bla
Abort trap: 6 (core dumped)

Perl version is v5.12.4


Solution

  • You can't. A core dump is fatal, so it is game over at that point. The proper solution would be to make sure that the code doesn't crash, but if you have no control over that C++ library that's going to be difficult.

    Perhaps there's the possibility of a workaround by forking your script before you're calling in to the library and then restarting if it does crash, but that's not a solution I'd ever consider using in production anywhere.