Can a C program handle C++ exceptions?

后端 未结 5 1534
醉话见心
醉话见心 2020-12-10 11:27

I am developing C++ component dll that can be used by C or C++ applications. The exposed dll functions are as follows

#include 
#ifdef IMPORT
         


        
5条回答
  •  粉色の甜心
    2020-12-10 12:17

    Only propagate exceptions to the caller if the caller is designed to handle them. I guess in your case terminate() will be called immediately once any exception escapes C++ code because from the point of C++ runtime that exception has not been handled.

    The same situation arises in COM servers design - the clients can be in whatever language/technology. The rul is that no exceptions should escape COM server methods - all exceptions must be caught and translated in HRESULT and (optionally) IErrorInfo. You should do similarly in your situations.

    In case C code is sandwiched between two layers of C++ code propagating exceptions to C code is still a very bad idea.

提交回复
热议问题