I'm implementing callbacks in C++ which will be called from ordinary C code. My main() function is C++ already, but C code will be responsible for creating threads that will eventually call my callbacks.
Right now my callbacks look like
int handle_foo(void *userdata) {
try {
MyCPPClass *obj = static_cast<MyCPPClass *>(userdata);
obj->doStuff();
return 0; // no error
} catch(...) {
LogError("doStuff failed");
return -1; // error
}
}
This works OK, but it seems weird to me. Furthermore, I lose some useful features such as the ability to find out what was thrown (without adding huge amounts of extra catch
statements to each and every one of my callbacks).
Is try {} catch(...) {}
here reasonable, or is there a better way to write my C callbacks?