来自第三方静态库的回调中的未捕获异常 [英] Uncaught exception in a callback from a 3rd party static library

查看:217
本文介绍了来自第三方静态库的回调中的未捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第三方库编译我的程序。如果内部发生错误,那个库包含错误回调。里面的错误回调我抛出一个异常,我有一个单元测试来验证,当我做一些无效的异常抛出。这一切都在Windows中工作得很漂亮,但是当我在linux(fedora)测试这个时候,我从一个未捕获的异常中得到了一个中止。

I am compiling my program with a 3rd party library. That library contains an error callback if an error occurs internally. Inside that error callback I am throwing an exception and I have a unit test to verify that when I do something invalid that the exception is thrown. This all works beautifully in Windows, but when I test this in linux (fedora) I am getting an abort from an uncaught exception.

try-catch块但没有运气。 (另外,我的所有代码都运行在谷歌测试框架,它也通常捕获异常)。看来捕获异常的唯一的东西是如果我在try块中直接在错误回调中包装throw语句。

I tried wrapping my call directly with a try-catch block but no luck. ( Also, all my code is running within the google test framework which also normally catches exceptions ). The only thing that seems to catch the exception is if I wrap the throw statement in a try block directly within the error callback.

有没有人知道为什么会发生这种情况如果有办法捕获异常?

Does anyone have any idea why this would happen and if there is a way to catch the exception?

推荐答案

当你与第三方库的接口,您通常必须捕获​​代码和代码之间的边界上的所有异常:

When you interface with third-party libraries you usually have to catch all exception on the border between your code and their code:

int yourCallback( params )
{
    try {
      doStuff( params );
      return Okay;
    } catch (...) {
      return Error;
    }
}

原因是您无法确定库是用C ++编写的,或者使用与你的代码使用非常相同的C ++运行时版本。

The reason is you can't be sure that library is written in C++ or it uses the very same version of C++ runtime as your code uses.

除非你完全确定代码可以处理你的异常, t将异常传播到第三方代码。极端的例子是COM,其中你的代码和其他代码可以是任何语言和任何运行时,你不允许让异常传播通过COM边界。

Unless you're completely sure that code can deal with your exceptions you can't propagate exceptions to third-party code. The extreme example is COM where both your code and "other code" can be in whatever language and with whatever runtime and you are not allowed to let exceptions propagate through COM boundary.

这篇关于来自第三方静态库的回调中的未捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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