简体   繁体   English

试图在MFC的CString :: Format中捕获异常

[英]Trying to catch exception in MFC's CString::Format

I am working with a C++ project (that I was not the author of) that has a lot of MFC string formatting functions. 我正在使用一个C ++项目(不是我的作者),该项目具有许多MFC字符串格式化功能。 Unfortunately, stuff like %d and %s are very close together (including the location of letters d and s on the keyboard) that one can be transposed with another. 不幸的是,诸如%d%s类的东西非常接近(包括字母d和s在键盘上的位置),一个可以与另一个换位。 So I may at times witness a code line as such: 因此,我有时可能会看到这样的代码行:

CString s;
s.Format(L"Value v=%s", 100);    //Should've been %d instead

This results in a hard crash of the process, that is very hard to locate & isolate in the final project. 这会导致过程的严重崩溃,很难在最终项目中定位和隔离。 So I was thinking to wrap the Format function in my own override and catch the exception & log it before it is thrown as unhandled exception. 所以我在考虑将Format函数包装在我自己的重写中,并捕获异常并在将其记录为未处理的异常之前对其进行记录。

So I employed the following construct: 因此,我采用了以下构造:

__try
{
    //Do the Format function here
}
__except(1)
{
    //Log the error, etc.
}

But unfortunately the construct above did not catch the exception from the first code chunk, so I got VS 2008 C++ debugger kick in and show this: 但是不幸的是,上面的构造没有从第一个代码块中捕获异常,因此我加入了VS 2008 C ++调试器并显示了此代码:

在此处输入图片说明

I then tried this: 然后我尝试了这个:

try
{
    //Do the Format function here
}
catch(int e)
{
    //Do the logging
}

But that didn't catch it either. 但这也没有抓住。

So how can I catch that fault? 那么我该如何捕捉到这个错误呢?

PS. PS。 And I have a second question. 我还有第二个问题。 Is there an easy way to override an MFC function, like Format for instance? 有没有简单的方法可以覆盖MFC函数,例如Format

MFC throws CException pointers, so you could try this: MFC抛出CException指针,因此您可以尝试以下操作:

try
{
    // Do the Format function here
}
catch(CException* e)
{
    // Do the logging then free the exception
    if (m_bThrowExceptionAgain)
        throw; // Do not delete e 
    else 
        e->Delete();
}

You have to delete the exception object once you have caught it as shown in the example. 捕获异常对象后,您必须删除它,如示例所示。 Also make sure you have C++ exceptions enabled in your compiler. 还要确保在编译器中启用了C ++异常。 See http://msdn.microsoft.com/en-us/library/0e5twxsh.aspx for more information. 有关更多信息,请参见http://msdn.microsoft.com/zh-cn/library/0e5twxsh.aspx

As others have already said low-level exceptions (like access violations) are not the same as C++ exceptions. 正如其他人已经说过的那样,低级异常(如访问冲突)与C ++异常不同。 They fall under the term Structured Exception Handling and would require other means to catch, at least by default. 它们属于术语“ 结构化异常处理” ,并且至少在默认情况下,将需要其他方式进行捕获。

It's possible to change compiler settings (at least in Visual Studio) to make it wrap those exceptions into something that C++ try/catch statements can handle, but as I recall that loses the details of what the SEH exception was and where it came from. 可以更改编译器设置(至少在Visual Studio中),以使其将那些异常包装到C ++ try / catch语句可以处理的内容中,但是正如我记得的那样,这丢失了SEH异常的含义以及其来源的详细信息。

One way or another you could probably get exceptions to work well enough to help track down these issues, but there is also another way: Use static code analysis. 您可能会以一种或另一种方式使异常正常运行,以帮助跟踪这些问题,但是还有另一种方式:使用静态代码分析。

While standard C++ compilers don't normally verify format/printf-style calls, there are various tools that will. 尽管标准的C ++编译器通常不会验证格式/ printf样式的调用,但可以使用多种工具。 In fact some recent versions/editions of Visual Studio come with a code analysis tool , although it may not have been available in VS 2008 which you mentioned. 实际上,Visual Studio的某些最新版本/版本附带了代码分析工具 ,尽管您提到的VS 2008中可能没有提供该工具 So it might be worthwhile for you to do some research and see if you can get a hold of some kind of code analysis tool which could then catch all the CString::Format mistakes during analysis/compile-time rather than run-time. 因此,可能值得您进行一些研究,看看您是否拥有某种代码分析工具,然后可以在分析/编译时而不是运行时捕获所有CString :: Format错误。

You can use _set_se_translator() to convert SEH exceptions like access violation to C++ exceptions which you can then catch with except() . 您可以使用_set_se_translator()来SEH异常转换像访问冲突C ++异常,然后可以赶上与except()

Some sample code: http://www.codeproject.com/Articles/422/SEH-and-C-Exceptions-catch-all-in-one 一些示例代码: http : //www.codeproject.com/Articles/422/SEH-and-C-Exceptions-catch-all-in-one

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM