Code reuse in exception handling

后端 未结 6 1810
无人共我
无人共我 2020-12-08 08:26

I\'m developing a C api for some functionality written in C++ and I want to make sure that no exceptions are propagated out of any of the exported C functions.

The s

6条回答
  •  青春惊慌失措
    2020-12-08 08:47

    It would be a shame to loose error information at the language boundary. You really should try to translate all exceptions into an error code usable from C.

    How you do it really depends on what your exception classes look like. If you control your exception class hierarchy, you can ensure that each class provides a translation using a virtual method. If not, you may still find it practical to use a translator function and test the types of the 'std::exception'-derived exception it receives to translate it into an error code, much like Jem suggested (remember: thrown exceptions will hurt performance anyway, so don't worry about the translation being slow).

提交回复
热议问题