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

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

问题描述

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



我试图用一个try-catch块,但没有运气。 (此外,我的所有代码都在谷歌测试框架内运行,通常也会捕获异常)。似乎捕获异常的唯一方法是如果我将throw语句直接包含在错误回调中的try块中。



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

解决方案

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

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

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



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


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.

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;
    }
}

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.

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天全站免登陆