在外部链接的C库异常传播 [英] exception propagation in externally linked C libraries

查看:118
本文介绍了在外部链接的C库异常传播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写++库,它使用外部第三方C库的C值。所以,我的图书馆将调用函数在这个第三方库和第三方库将回调到我的媒体库中的不同部分。

我想知道发生了什么异常在这种情况下?
说MyLib中:: foo的()调用外部C库函数最终调用MyLib中::杆(),和酒吧抛出一个异常,会发生什么?将异常正确地传播到处理器中富()?

谢谢!


解决方案

  

将异常正确地传播到处理器中富()


我觉得异常无论是通过外部C code传播是不确定的。更糟的是,在C code是未prepared,无法处理异常。 C code没有需要对突然的,意外的回报免疫,所以它是没有RAII等。

在我曾经面临这样的情况,我回到C API之前捕捉到的异常,它存储,并重新把它扔一次调用来自于C API回来。

I am writing a C++ library that uses an external third party C library. So my library will call functions in this third party library and the third party library will call back into a different part of my library.

I am wondering what happens to exceptions in this case? Say MyLib::foo() calls external C library function which eventually calls MyLib::bar(), and bar throws an exception, what happens? Will the exception be correctly propagated to a handler in foo() ?

Thanks!

解决方案

Will the exception be correctly propagated to a handler in foo()?

I think whether exceptions propagate through external C code is undefined. What's even worse, the C code is unprepared and unable to handle the exception. C code doesn't need to immune against sudden, unexpected returns, so it knows no RAII etc.

When I was once faced with such a situation, I caught the exception before returning to the C API, stored it, and re-threw it once the call came back from the C API.

这篇关于在外部链接的C库异常传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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