可以在C程序处理C ++异常? [英] Can a C program handle C++ exceptions?

查看:96
本文介绍了可以在C程序处理C ++异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的C ++可以通过C或C ++应用程序中使用组件DLL。
暴露的DLL函数如下

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

#include <tchar.h>
#ifdef IMPORT
#define DLL __declspec(dllimport)
#else 
#define DLL __declspec(dllexport)
#endif

extern "C" {

    DLL  bool __cdecl Init();
    DLL  bool __cdecl Foo(const TCHAR*);
    DLL  bool __cdecl Release();

}

在内部实现的这些功能​​是未暴露的C ++类,我想用这种风格的DLL可以用C或C ++应用程序中使用。
现在的问题是,我不处理任何类型的C ++异常(即bad_alloc),我离开了这个东西给调用者(高层)。
经过与我的同事很多争论,我应该捕获所有异常并返回错误$ C $℃或至少假的,因为在C应用程序无法处理C ++异常的情况下,?真的吗?和我一般应怎么办?有经验处理产生的异常,如果您正在开发组件将被其他系统中使用的规则。

the internal implementation of these functions are C++ classes which are not exposed, I assume using this style the dll can be used either in C or C++ apps. The problem is I do not handle any type of c++ exception (i.e. bad_alloc) and I left this stuff to the caller (the higher layer). After much debate with my colleagues that I should catch all the exceptions and return error code or at least false because in case of C application it can not handle C++ exceptions? is that true? and what I should do in general? is there a rule of thumb for handling exeptions if you are developing component that will be used by other system.

推荐答案

C没有异常,所以一般你应该捕获所有异常并返回一个错误code和/或提供返回信息的函数关于最后一个错误。

C doesn't have exceptions, therefore in general you should catch all exception and return an error code and/or provide a function that returns the information about the last error.

这篇关于可以在C程序处理C ++异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆