无法使用catch(...)捕获c ++异常 [英] Unable to catch c++ exception using catch (...)

查看:167
本文介绍了无法使用catch(...)捕获c ++异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方库有时候会抛出异常。所以我决定将我的代码包装在一个try / catch(...)中,以便我可以记录关于异常发生的信息(没有具体的细节,只是发生了。)



但由于某些原因,代码仍然崩溃。在客户端计算机上,它很难崩溃,而在catch(...)中记录异常的代码从未被执行。如果我在我的调试/开发机器上运行,我得到弹出窗口询问我是否要调试。当我这样做,它报告0xC0000005:访问冲突读取位置XXX。



奇怪的是,使用较旧版本的第三方库,完全相同的代码DOES捕获异常,并记录异常执行的代码。 (我在VS中验证了这一点,看到相同的条件发生。)



这是正在执行的伪代码:

  pObject = pSystem-> Get_pObject()
pSystem-> DoSomethingThatMightDestroy_pObject();
尝试
{
/ *调用抛出异常的第三方函数* /
pObject-> SetValue(0);
}
catch(...)
{
__DEBUG_LOG_POSITION__; //用于记录当前文件行的宏
//此代码用于在旧版本的第三方库
//中运行,但较新版本只是在运行catch(...之前崩溃) )
}

所以我有两个问题:


  1. 第三方是否有可能编辑库的方式发生变化,以致我的代码无法捕获异常? (是的,有机会让第三方做出任何必要的修复,如果我知道要告诉他们,重新编译)。


  2. 假设我无法让第三方解决它,我该怎么办才能捕捉这些例外?我正在考虑...有一些方法可以确定pObject是否被释放?



解决方案

AFAIK访问冲突不要抛出异常...至少不是标准的!



也许捕捉Windows特定的本机异常有助于: http://www.gamedev.net/reference/articles/article2488.asp


I have a third-party library that is sometimes throwing an exception. So I decided to wrap my code in a try/catch(...) so that I could log information about the exception occurring (no specific details, just that it happened.)

But for some reason, the code still crashes. On client computers, it crashes hard and the code to log the exception in the catch(...) never gets executed. If I run this on my debug / development machine I get the popup asking me if I want to debug. When I do this, it reports 0xC0000005: Access violation reading location XXX.

The odd thing is that with an older version of the third-party library, the exact same code DOES catch the exception, and the code to log the exception DOES execute. (I verified this within VS watching the same conditions occur.)

Here's the pseudo-code that is executing:

pObject = pSystem->Get_pObject()
pSystem->DoSomethingThatMightDestroy_pObject();
try
{
    /*   Call to third party function that is throwing exception */
    pObject->SetValue(0);
}
catch (...)
{
    __DEBUG_LOG_POSITION__;  // A macro to log the current file line
    //  This code used to run in the older version of third-party library
    //  but the newer version just crashes before running the catch(...)
}

So I have two questions:

  1. Is there some change in the way the third party might have compiled the library so that my code wouldn't be able to catch the exception? (Yes, there is a chance I can get the third party to make whatever fixes are necessary and recompile for me, if I know what to tell them.)

  2. Assuming I can't get the third party to fix it, what can I do to catch these exceptions? I'm thinking along the lines of... is there some way for me to determine whether pObject was deallocated?

解决方案

AFAIK access violation don't throw exception... at least not standard ones!

Maybe catching windows-specific "native" exception would help : http://www.gamedev.net/reference/articles/article2488.asp

这篇关于无法使用catch(...)捕获c ++异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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