MFC中的异常处理 [英] Exception Handling in mfc

查看:109
本文介绍了MFC中的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试在以下代码中处理异常

Hi,
I am trying to handle exception in the following code

CStringArray StrArray;
StrArray.RemoveAll();


try
{
    CString str;
    str = StrArray.ElementAt(1);
}
catch(...)
{
    AfxMessageBox("No Element Present at this Position");
    throw;
}



但是对于上述情况,不是直接捕获该位置不存在任何元素的断言,而是直接引发了VC ++内置数组大小的调试断言,而没有进入catch块.
注意:-我想在Debug模式而不是Release模式下执行此代码
问候,
Madhukar Parab



But for the above case instead of catching the assertion of no element present at that position it directly throws an VC++ Built in Debug Assertion of Array Size without entering the catch block.
Note:- I want to execute this code in Debug Mode and not in Release mode
Regards,
Madhukar Parab

推荐答案

没有诸如调试"或发布"模式之类的东西.这些只是配置的名称.这些配置仅用于一次切换构建和其他选项的组,它们只是这些选项组的名称,具体取决于*.* proj文件的模式(不同类型的项目完全相同).如果应用程序是使用任何一种配置编译的,则可以对其进行调试.

解决问题之前,您应该了解这些配置.希望对项目和构建有更好的了解将对您有所帮助.关于该问题,请参见Visual Studio中的选项:[主菜单] =>调试=>例外.

—SA
There is no such thing as Debug or Release mode. These are just names of configurations. The configurations are just used to switch groups of build and other options at once, they are just the names of such groups of options, according the the schema of *.*proj files (which is exactly the same for different types of projects). You can debug your application if it was compiled with either configuration.

Before addressing your problem, you should understand those configurations. I hope better understanding of the project and build will help you. As to the problem, please see the options in Visual Studio: [Main menu] => Debug => Exceptions.

—SA


在尝试使用GetUpperBound()
访问大小之前,只需检查其大小即可.
Just check the size before you try to access it using GetUpperBound()
CStringArray arr;
arr.RemoveAll();

CString tst;
int i=1; //for testing

if( i<= arr.GetUpperBound())
	tst = arr.ElementAt(i);



您还可以使用GetSize(),只记得在一个空列表上将为您提供零,而GetUpperBound将为您提供-1(大小为您提供元素数量,而UpperBound为您提供最大索引)



You can also use GetSize(), just remember that on an empty list that''s going to give you zero where GetUpperBound will give you a -1 (size gives you the number of elements where UpperBound gives you the maximum index).


忽略-SA的注释,当然有DEBUG和RELEASE模式,IDE通过默认为您生成这两种模式,还有DEBUG和RELEASE库,它们是不同的,当然还有大量的代码项目文章,描述了DEBUG和RELEASE模式之间的区别.

而且,您可以使用DEBUG和RELEASE配置的设置来创建自己的配置"或废话,使其不再类似于IDE设置的默认值.但是,默认的DEBUG和RELEASE模式配置在开发过程中已根深蒂固,以至于它们具有自己的含义,并存在于我们的公共/公共内存中.

对于您的问题,DEBUG模式库使用断言宏来捕获对这些例程的调用中的常见问题.在调试中,它们旨在帮助您通过有意义的消息来捕获这些问题,而不是捕获空指针异常"或其他必须调查的晦涩问题.

因此,尽管它们在这方面有所帮助,但它们确实剥夺了您捕获"异常的能力.

这些断言中有许多确实允许您继续",即使用错误的值/参数来恢复代码.通常,这样做会引发您期望捕获的异常.但是请不要忘记调试器也在尝试帮助您,因此无论如何您都可能会看到异常.

支付调试器为您提供的所有其他帮助的价格不菲.
Ignore -SA''s comment, of course there are DEBUG and RELEASE modes, the IDE generates both of those for you by defaule, there are DEBUG and RELEASE libraries, which are different, and, of course, tons of Code Project Articles describing the difference between DEBUG and RELEASE modes.

And you can create your own "configurations" or muck with the settings for DEBUG and RELEASE configurations enough that they no longer resemble the defaults set by the IDE. However, the default DEBUG and RELEASE mode configurations are so ingrained in development processes that they have taken on a meaning of their own and in our common / communal memory.

As to your problem, the DEBUG mode libraries use the assertion macros to catch common problems in calls to these routines *BEFORE* they blow up in unexpected ways and throw exceptions. In debugging, they are meant to help you catch these problems with meaningful messages instead of you catching the "null pointer exception" or other obscure problem that you''d have to investigate.

So, while they are helpful in that regard, they do take away your ability to "catch" the exception.

Many of these asserts do allow you to "continue", that is, resume the code using the bad value / parameter. Often, doing that will throw the exception you expect to catch. But don''t forget that the debugger is trying to help you there too so it may see the exception before you do anyway.

Small price to pay for all the other help the debugger gives you.


这篇关于MFC中的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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