try{ } catch(...) { } in C callback - bad idea?
c++, callback, exception
Solution
Yes, you have to catch the exceptions and hopefully translate them into something useful. Letting exceptions propagate through C code leads to undefined behavior. At best you cannot expect the C code to maintain consistent program state.
See this answer for an easy example. A harder example is with some complex piece of software such as SQLite - C code will grab some mutex and will not release it because the exception simply "flies through" and your program is now toast.
Also this has any chance of "working" if all the code is built against the same C++ runtime. If you happen to have callback implemented in say Visual C++ 9 and the rest of the code in say Visual C++ 10 or those parts are compiled against static runtime libraries - you now have two distinct runtimes and the unhandled exception in the callback causes `terminate()` being called.
Problem
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?